Linq 条件查询的一些方法
1.LINQ的IN: var queryResult = from p
in
db.Products
where (
new
int
?[] {1,2}).Contains(p.CategoryID)
select p;
2.LINQ的NOT IN: var queryResult = from p
in
db.Products
where !(
new
int
?[] { 1, 2 }).Contains(p.CategoryID)
select p;
3.LINQ的NOT IN 遍历集合
List<string> source = new List<string>{ "aaa", "bbb" };
List<string> px = new List<string> { "aaa", "bbb", "ccc" };
var q = from s in source
from p in px
where s==p
select s;
4.SQL中的in和not in语法对应的LINQ写法
List<testInfo> listAll = new List<testInfo>(); listAll.Add(new testInfo() { id = 1, name = "11", test = "111" }); listAll.Add(new testInfo() { id = 2, name = "22", test = "222" }); listAll.Add(new testInfo() { id = 3, name = "33", test = "333" }); listAll.Add(new testInfo() { id = 4, name = "33", test = "333" }); List<testInfo> listFind = new List<testInfo>(); listFind.Add(new testInfo() { id = 1, name = "44", test = "111" }); listFind.Add(new testInfo() { id = 2, name = "22", test = "222" }); listFind.Add(new testInfo() { id = 3, name = "33", test = "333" }); listFind.Add(new testInfo() { id = 4, name = "55", test = "333" }); //SQL中的in和not in语法对应的LINQ写法 //相当于查询语句:select * from listAll p where p.name not in(select f.name from listFind) var a = (from p in listAll where !(from f in listFind select f.name).Contains(p.name) select p).ToList();