LINQ不能用string.contains? [英] LINQ can't use string.contains?

查看:295
本文介绍了LINQ不能用string.contains?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

string queryString = "Marco".ToLower();
utenti = db.User.Where(p => 
        queryString.Contains(p.Nickname.ToLower()) ||
            queryString.Contains(p.Nome.ToLower()) ||
            queryString.Contains(p.Cognome.ToLower())).ToList();

但我得到:

仅可在客户端上被评估参数为String.Contains方法支持的

Only arguments that can be evaluated on the client are supported for the String.Contains method.

为什么呢?我无法使用。载()

Why? Can't I use .Contains()?

推荐答案

尝试 .IndexOf 。它不是LINQ不能做包含,这是LINQ到实体和LINQ to SQL不了的。

Try .IndexOf. It is not LINQ that can't do Contains, it's LINQ to Entities and LINQ to SQL that can't.

string queryString = "Marco";
utenti = db.User.Where(p => 
    queryString.IndexOf(p.Nickname, StringComparison.OrdinalIgnoreCase) >= 0 ||
        queryString.IndexOf(p.Nome, StringComparison.OrdinalIgnoreCase) >= 0 ||
        queryString.IndexOf(p.Cognom, StringComparison.OrdinalIgnoreCasee) >= 0)
.ToList();

为什么?

LINQ使用延迟执行。这意味着它会等待,直到你要遍历查询结果它做任何事情之前。有3种主要类型LINQ的:

LINQ uses deferred execution. This means it waits until you want to iterate over your query results before it does anything. There are 3 main types of LINQ:

  1. LINQ到对象 - 当你的的IEnumerable 已经在堆上
  2. LINQ到实体 - 当你想使用实体框架查询数据库
  3. 的LINQ to SQL - 当您想查询使用LINQ to SQL数据库
  1. LINQ to Objects - when your IEnumerable is already on the heap.
  2. LINQ to Entities - when you want to query a database using Entity Framework.
  3. LINQ to SQL - when you want to query a database using LINQ to SQL.

在第二个2的情况下延迟执行意味着您的查询未在数据库上执行,直到您列举了导致的foreach 块,或调用枚举方法如 .ToList .ToArray 等在那之前,你的查询只存储为EX pression树在存储器中。

Deferred execution in the context of the second 2 means that your query is not executed on the database until you enumerate the results in a foreach block, or invoke an enumeration method like .ToList, .ToArray, etc. Until then, your query is just stored as expression trees in memory.

您的查询将工作刚刚桃色如果 db.User 是内存中的一个集合。然而,当数据在数据库中,LINQ到实体(或LINQ到SQL)必须把你的前pression树其称之为店前pression - 这仅仅是看中聊转换我的LINQ EX pressions到SQL。

Your query would work just peachy if db.User was a collection in memory. However when the data is in a database, LINQ to Entities (or LINQ to SQL) must translate your expression trees to what it calls a "store expression" -- which is just fancy talk for "convert my LINQ expressions to SQL".

现在想象你有你想用你的查询自定义的C#算法,以及你做了这样的事情:

Now imagine you had a custom C# algorithm you wanted to use for your query, and you did something like this:

var result = db.User.Where(x => MyCustomMethod(x));

今天有没有办法,LINQ到实体可以转换你的C#code到一个SQL查询(店前pression)。这是同很多其他的C#方法,您依赖日常。它也不支持 .ToLower .ToUpper .StartsWith .EndsWith 等,还有就是可以转换存储EX pressions C#方法,数量有限和 .IndexOf 恰好是其中之一。

There is no way today that LINQ to Entities can convert your C# code into a SQL query (store expression). It is the same with a lot of other C# methods you rely on daily. It also does not support .ToLower, .ToUpper, .StartsWith, .EndsWith, etc. There is a limited number of C# methods that can be converted to store expressions, and .IndexOf just happens to be one of them.

但是请记住,这仅仅是个字符串对象的包含方法,我们这里所说的是不支持存储EX pressions。 LINQ到实体确实支持。载的IEnumerable 秒。以下是有效的,并会使用LINQ工作,以实体(不知道的LINQ to SQL):

However keep in mind that it is only the string object's Contains method that we are talking about here that is not supported for store expressions. LINQ to Entities does support .Contains on IEnumerables. The following is valid and will work with LINQ to Entities (not sure about LINQ to SQL):

var idsIWantToFind = new[] { 1, 2, 3 };
var users = db.Where(x => idsIWantToFind.Contains(x.UserId));

以上是在做一个SQL相当于其中userid IN(1,2,3) predicate。

The above is the equivalent of doing a SQL WHERE UserId IN (1, 2, 3) predicate.

这篇关于LINQ不能用string.contains?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆