LINQ to SQL查询可以使用NOT IN吗?
例如,SELECT au_lname,state FROM authors WHERE state NOT IN('CA','IN','MD')
Liststates = new List { "CA", "IN", "MD" }; var q = from a in authors where !states.Contains(a.state) select new { a.au_lname, a.state };
要么
var q = authors.Where( a => !states.Contains( a.state ) ) .Select( a => new { a.au_lname, a.state } );
你可以用Contains来做:
var states = new[] {"CA", "IN", "MD"}; var query = db.Authors.Where(x => !states.Contains(x.state));
这是一个例子:
NorthwindDataContext dc = new NorthwindDataContext(); dc.Log = Console.Out; var query = from c in dc.Customers where !(from o in dc.Orders select o.CustomerID) .Contains(c.CustomerID) select c; foreach (var c in query) Console.WriteLine( c );