Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq function like .Net string.CompareOrdinal

I need to compare strings using the string.CompareOrdinal(...) inside a linq query.

string max;
string min;

var res = db.Table
            .Where(c => string.CompareOrdinal(c.Id,  min) >= 0)
            .Where(c => string.CompareOrdinal(c.Id,  max) <= 0)
            .ToList();

The code throws a exception:

LINQ ti Entities does not recongnize the method 'Int32 CompareOrdinal(System.String, System.String)' method, and this method cannot be translated into a store expression.

There are a lot of data in the table, so I really need the where clause.

Is there a way around this?

Update

I'm not trying to deside if two strings are equal - case sensitive or not.

I'm trying to deside whether a string is within a range. So the quistion is

  • Is there a way to do that - so that is works with L2E?

Obviously, I cannot use the string.CompareOrdinal

like image 560
Jens Kloster Avatar asked Apr 03 '13 10:04

Jens Kloster


1 Answers

A colleague of mine found a workaround using string.Compare instead of string.CompareOrdinal

string min = "a";
string max = "z";

 var res = db.Table
         .Where(c => string.Compare(c.Id, min, StringComparison.OrdinalIgnoreCase) >= 0)
         .Where(c => string.Compare(c.Id, max, StringComparison.OrdinalIgnoreCase) <= 0)
         .ToList();

this is the generated SQL:

SELECT 
[Extent1].[Id] AS [Id]
FROM [dbo].[Table] AS [Extent1]
WHERE ([Extent1].[Id] >= 'a') AND ([Extent1].[Id] <= 'z')
like image 135
Jens Kloster Avatar answered Oct 15 '22 09:10

Jens Kloster