[英]What is LINQ equivalent of SQL
List<string> li = new List<string>();
li.Add("10,11,12,1");
li.Add("10,11,12,13");
var q = from d in li
where d.Contains("1")
select d;
我有一個帶有兩個值的字符串列表。 我有單個值“ 1”,我想獲取字符串中包含值“ 1”的列表對象,即我想要列表中的第一個對象。 如果我使用contains,它將返回兩個列表對象的值,那么查詢將給我帶來完美的結果嗎?
嘗試將位置更改為
where d.Split(',').Contains("1")
這會將字符串拆分為一個項目列表,然后檢查該項目是否在該列表中。
編輯:
如注釋中所述,您不需要ToList()
因此請更改上面的內容。
如果要直接對數據庫運行此查詢,則必須執行以下操作:
var q = from d in li
where d.StartsWith("1,") || d.EndsWith(",1") || d.Contains(",1,")
|| d.Equals("1")
select d;
否則,其他答案將在內存中起作用
您可以嘗試以下方法:
List<string> li = new List<string>();
li.Add("10,11,12,1");
li.Add("10,11,12,13");
string searchItem = "1";
var q = from d in li
where ("," + d + ",").Contains("," + searchItem + ",")
select d;
該查詢也將直接針對數據庫工作。
嘗試這個:
var q =
from d in li
where d.Split(',').Any(x => x == "1")
select d;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.