Linq function like .Net string.CompareOrdinal

后端 未结 2 698
陌清茗
陌清茗 2021-01-06 01:29

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

string max;
string min;

var res = db.Table
            .Whe         


        
2条回答
  •  别那么骄傲
    2021-01-06 02:26

    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')
    

提交回复
热议问题