久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

LINQ 不能使用 string.contains?

LINQ can#39;t use string.contains?(LINQ 不能使用 string.contains?)
本文介紹了LINQ 不能使用 string.contains?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這是我的代碼:

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 方法只支持可以在客戶端計算的參數(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:

  1. LINQ to Objects - 當(dāng)您的 IEnumerable 已經(jīng)在堆上時.
  2. LINQ to Entities - 當(dāng)您想使用實體框架查詢數(shù)據(jù)庫時.
  3. 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 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));

以上相當(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應(yīng)該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
主站蜘蛛池模板: 一级免费毛片 | 精品视频一区二区三区 | 中文字幕人成乱码在线观看 | www.日韩免费 | 色综合久久88色综合天天 | 欧美影院久久 | 91精品国产乱码麻豆白嫩 | 久久国产精品色av免费观看 | 性色视频 | 一区二区在线不卡 | 欧美激情一区二区 | 国产精品精品视频一区二区三区 | 国产高清在线精品一区二区三区 | 久久av一区二区 | 日韩在线精品视频 | 亚洲欧美aⅴ | 欧美福利一区 | 国产一区二 | 香蕉久久a毛片 | 精品久久视频 | 亚洲免费在线观看 | 男女视频免费 | 狠狠涩 | 欧美一区免费 | 亚洲成人精品一区二区 | 第四色播日韩第一页 | 亚洲精品68久久久一区 | 国产成人免费网站 | 欧美高清视频一区 | 日日操天天射 | 国精产品一品二品国精在线观看 | 久久国产精品网 | 成人三级视频 | 亚洲视频在线观看一区二区三区 | 成人精品在线观看 | 狠狠狠干 | 欧美一区免费在线观看 | 国产成人综合在线 | 日韩久草 | 欧美三级久久久 | 一区二区精品在线 |