LINQ to SQL语句(7)之Exists/In/Any/All/Contains
系列文章导航:
LINQ to SQL语句(2)之Select/Distinct
LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
LINQ to SQL语句(6)之Group By/Having
LINQ to SQL语句(7)之Exists/In/Any/All/Contains
LINQ to SQL语句(8)之Concat/Union/Intersect/Except
LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods
LINQ to SQL语句(12)之Delete和使用Attach
LINQ to SQL语句(14)之Null语义和DateTime
LINQ to SQL语句(19)之ADO.NET与LINQ to SQL
Not Contains则取反:
var q = ( from o in db.Orders where !( new string[] { "AROUT", "BOLID", "FISSA" }) .Contains(o.CustomerID) select o).ToList();
1.包含一个对象:
var order = (from o in db.Orders where o.OrderID == 10248 select o).First(); var q = db.Customers.Where(p => p.Orders.Contains(order)).ToList(); foreach (var cust in q) { foreach (var ord in cust.Orders) { //do something } }
语句描述:这个例子使用Contain查找哪个客户包含OrderID为10248的订单。
2.包含多个值:
string[] cities = new string[] { "Seattle", "London", "Vancouver", "Paris" }; var q = db.Customers.Where(p=>cities.Contains(p.City)).ToList();
语句描述:这个例子使用Contains查找其所在城市为西雅图、伦敦、巴黎或温哥华的客户。