Linq函数如.Net string.CompareOrdinal [英] Linq function like .Net string.CompareOrdinal
问题描述
我需要使用 string.CompareOrdinal(...)
在linq查询中比较字符串。
string max;
string min;
var res = db.Table
.Where(c => string.CompareOrdinal(c.Id,min)> = 0)
。 ; string.CompareOrdinal(c.Id,max)< = 0)
.ToList();
代码引发异常:
LINQ ti实体不识别方法Int32
CompareOrdinal(System.String,System.String)方法,并且此方法
不能转换为存储表达式。 / p>
表中有很多数据,所以我真的需要其中
p>
我不是如果两个字符串相等 - 区分大小写,不设置。
我想要确定字符串是否在范围内。
所以这个问题是
- 有没有办法做到这一点 - 这是L2E的工作?
显然,我不能使用 string.CompareOrdinal
我的一个同事发现了一个解决方法,使用 string.Compare
,而不是 string.CompareOrdinal
string min =a;
string max =z;
var res = db.Table
.Where(c => string.Compare(c.Id,min,StringComparison.OrdinalIgnoreCase)> = 0)
。 (c => string.Compare(c.Id,max,StringComparison.OrdinalIgnoreCase)< = 0)
.ToList();
这是生成的SQL:
SELECT
[Extent1]。[Id] AS [Id]
FROM [dbo] 。[Id]> ='a')AND([Extent1]。[Id] <= z')
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
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')
这篇关于Linq函数如.Net string.CompareOrdinal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!