使用LINQ, Lambda達到SQL in 多欄位寫法
使用LINQ, Lambda達到SQL in 多欄位寫法
不能直接對SQL LINQ使用Contains
List<string> list_Name = new List<string>() { "Alex", "Peter"};
//不支援Contains語法
//var query = from t1 in db.Table1
// where list_Name.Contains(t1.Name)
// select t1;
var query = from t1 in db.Table1
select t1;
//query = query.Where(x => list_ima01.Contains(x.Ima01)); //不支援Contains語法
query = query.where(x => list_Name.Any(a => a == x.Name); //改用Any
var list = query.ToList();
先把SQL LINQ 進行ToList為Object後, 就可以使用Contains
List<string> list_AAA = new List<string>() { "123", "456", "789"};
var query = from t1 in AAA
select t1;
var list = query.ToList(); //先ToList
var q2 = list.Where(x => list_AAA.Contains(x.A)); //就可以對Object LINQ使用Contains
var list_q2 = q2.ToList();
留言
張貼留言