C# Rule : Culture should be specified for "string" operations


(Vish S.) #1

Hi All-
The sonarqube C# rule :Culture should be specified for “string” operations. reported some issues in our project which are related to LINQ to SQL queries. And when we try to fix this type of issues by adding cultureinfo in string conversion process , we are getting the run time exception. Below is the sample code and exception details.

Code Sample:

Dim londonCustomers = From customer in Customers _
Where customer.City = “London” && customer.code.ToUpper(CultureInfo.InvariantCulture).Trim()==“Somthing”
Select customer

Exception Details:
NotSupportedException was unhandled by user code.
Additionalinfo : LINQ to Entities does not recognize the method ‘System.String.ToUpper(System.Globalization.CultureInfo)’ method and this method can not be translated in to a store expression.

So do you think , in this scenario of LINQ to SQL the issues detected by this rule can be marked as false +ve?

(Valeri Hristov) #2

You are right, the rules should not recommend changes that lead to not working code. Ticket created: https://github.com/SonarSource/sonar-csharp/issues/1841

(Vish S.) #3

Thank you for the response!