简体   繁体   English

不区分大小写的数组,包含在Linq查询中

[英]Case Insensitive Array.Contains in Linq Query

I'm trying to use Array.Contains on a string array within a Linq query: 我正在尝试在LINQ查询中的字符串数组上使用Array.Contains:

var otherMatchingDevices = from d in selectedDevices
                           from c in mldb.Companies
                           where d.CompanyID == c.CompanyID && c.Deleted == 0
                           where searchTerms.Contains(d.Name.ToString(), StringComparer.CurrentCultureIgnoreCase) || searchTerms.Contains(c.CompanyName.ToString(), StringComparer.CurrentCultureIgnoreCase)
                           select d;

When the query is evaluated it crashes with "Unsupported overload used for query operator 'Contains'. 评估查询时,它崩溃,并显示“用于查询运算符'包含'的不支持的重载。

I tested this code using the StringComparer and it works fine and prints out "fOO": 我使用StringComparer测试了此代码,它可以正常工作并打印出“ fOO”:

string[] sList = { "fOO", "bar" };
string[] array = { "foo" };
List<string> stringlist = sList.ToList();
var qry = from s in stringlist
          where array.Contains(s, StringComparer.CurrentCultureIgnoreCase)
          select s;
if (qry.Count() > 0) Console.WriteLine(qry.First().ToString());

Can anyone show me how to use a case insensitive Array.Contains within a Linq query? 任何人都可以告诉我如何使用不区分大小写的Array.Linq查询中包含吗? I do NOT want to convert the original string ToUpper() or ToLower() as it is expensive and it changes the original data. 我不想转换原始字符串ToUpper()或ToLower(),因为它很昂贵并且会更改原始数据。

Your first snippet is invoked using Linq to SQL , this means that it will eventually be translated into SQL. 您的第一个代码段是使用Linq to SQL调用的,这意味着最终将其转换为SQL。 So, whether comparison will be case-sensitive or not depends on the COLLATION of your table column. 因此,比较是否区分大小写取决于表列的COLLATION That's why Linq throws the exception, because it cannot guarantee case-sensitivity. 这就是Linq抛出异常的原因,因为它不能保证区分大小写。

Your second query snippet is performed using Linq to Objects , hence string equality can be enforced, since the actual string is already in-memory. 您的第二个查询代码段是使用Linq to Objects执行的,因此可以执行字符串相等性,因为实际的string已经在内存中。

I tested this code using the StringComparer and it works fine and prints out "fOO": 我使用StringComparer测试了此代码,它可以正常工作并打印出“ fOO”:

LINQ to Objects is different than LINQ to SQL/Entities. LINQ to Object与LINQ to SQL / Entities不同。 The latter needs to convert your query into a SQL expression, hence it doesn't understand what StringComparsion is. 后者需要将查询转换为SQL表达式,因此它不了解StringComparsion是什么。

You can use AsEnumerable() on your query to bring the data in-memory: 您可以在查询中使用AsEnumerable()将数据带入内存中:

var otherMatchingDevices = (from d in selectedDevices
                            from c in mldb.Companies
                            where d.CompanyID == c.CompanyID && c.Deleted == 0)
                            .AsEnumerable()
                            .Where(searchTerms.Contains(d.Name.ToString(), 
                                               StringComparer.CurrentCultureIgnoreCase) || 
                                   searchTerms.Contains(c.CompanyName.ToString(), 
                                               StringComparer.CurrentCultureIgnoreCase))
                            .ToArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM