Linq function like .Net string.CompareOrdinal

馋奶兔 提交于 2019-12-01 06:27:37

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

If Id is a string, this solution works, so it looks like Id is an int. Int cannot be compared to a string. Change Id to a string or min/max to an int to make it work (and use a simple < and > between the int values).

By the way: it would save 1 iteration to check for min and max in 1 where function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!