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

LINQ 不能使用 string.contains?

LINQ can#39;t use string.contains?(LINQ 不能使用 string.contains?)
本文介紹了LINQ 不能使用 string.contains?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(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 方法只支持可以在客戶端計(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:

  1. LINQ to Objects - 當(dāng)您的 IEnumerable 已經(jīng)在堆上時.
  2. LINQ to Entities - 當(dāng)您想使用實(shí)體框架查詢數(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)用)
主站蜘蛛池模板: 日韩精品免费视频 | 国产精品久久久久久久毛片 | 国产精品1区2区3区 国产在线观看一区 | 日本精品视频一区二区 | 国产精品亚洲综合 | 久久在线| 亚洲精品9999久久久久 | 无码一区二区三区视频 | 色888www视频在线观看 | 午夜精品一区二区三区在线 | 亚州影院 | 精品国产伦一区二区三区观看说明 | 精品国产青草久久久久福利 | 99久久99 | 91久久国产综合久久91精品网站 | 亚洲视频免费在线 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 国产一级网站 | 精品乱码一区二区 | 久久久久国产精品一区 | 天堂在线免费视频 | 性天堂网 | 亚洲理论在线观看电影 | 国内精品久久久久久久影视简单 | 午夜久久av | 欧美日韩国产一区二区三区 | 美女视频一区 | 色综合色综合色综合 | 97超碰成人 | 波多野结衣一区二区三区在线观看 | 亚洲欧美日韩在线 | 日本电影韩国电影免费观看 | 日韩欧美在线视频播放 | 不卡一二三区 | 日韩av电影院 | 欧洲视频一区二区 | 成人av电影网 | 成人福利网 | 精品美女视频在免费观看 | 在线91 | 久久com|