問題描述
這是我的代碼:
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 方法只支持可以在客戶端計(jì)算的參數(shù).
Only arguments that can be evaluated on the client are supported for the String.Contains method.
為什么?我不能使用 .Contains()
嗎?
Why? Can't I use .Contains()
?
推薦答案
試試 .IndexOf
.不是 LINQ 不能做 Contains
,而是 LINQ to Entities 和 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 使用延遲執(zhí)行.這意味著它會等到您想要迭代查詢結(jié)果,然后再執(zhí)行任何操作.LINQ 有 3 種主要類型:
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:
- LINQ to Objects - 當(dāng)您的
IEnumerable
已經(jīng)在堆上時. - LINQ to Entities - 當(dāng)您想使用實(shí)體框架查詢數(shù)據(jù)庫時.
- LINQ to SQL - 當(dāng)您想使用 LINQ to SQL 查詢數(shù)據(jù)庫時.
在第二個 2 的上下文中延遲執(zhí)行意味著您的查詢不會在數(shù)據(jù)庫上執(zhí)行,直到您在 foreach
塊中枚舉結(jié)果,或調(diào)用像 .ToList 這樣的枚舉方法
、.ToArray
等.在此之前,您的查詢只是作為表達(dá)式樹存儲在內(nèi)存中.
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
是內(nèi)存中的一個集合,那么您的查詢將正常工作.但是,當(dāng)數(shù)據(jù)位于數(shù)據(jù)庫中時,LINQ to Entities(或 LINQ to SQL)必須將您的表達(dá)式樹轉(zhuǎn)換為它所謂的存儲表達(dá)式"——這只是將我的 LINQ 表達(dá)式轉(zhuǎn)換為 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".
現(xiàn)在假設(shè)您有一個想要用于查詢的自定義 C# 算法,并且您執(zhí)行了以下操作:
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 to Entities 無法將您的 C# 代碼轉(zhuǎn)換為 SQL 查詢(存儲表達(dá)式).這與您每天依賴的許多其他 C# 方法相同.它也不支持 .ToLower
、.ToUpper
、.StartsWith
、.EndsWith
等.有一個可轉(zhuǎn)換為存儲表達(dá)式的 C# 方法數(shù)量有限,而 .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.
但是請記住,只有我們在這里討論的字符串對象的 Contains
方法不支持 store 表達(dá)式.LINQ to Entities 在 IEnumerable
上支持 .Contains
.以下內(nèi)容有效并且適用于 LINQ to Entities(不確定 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 IEnumerable
s. 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));
以上相當(dāng)于做一個 SQL WHERE UserId IN (1, 2, 3)
謂詞.
The above is the equivalent of doing a SQL WHERE UserId IN (1, 2, 3)
predicate.
這篇關(guān)于LINQ 不能使用 string.contains?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!