当前位置:  开发笔记 > 数据库 > 正文

LINQ to SQL查询使用"NOT IN"

如何解决《LINQtoSQL查询使用"NOTIN"》经验,为你挑选了3个好方法。

LINQ to SQL查询可以使用NOT IN吗?

例如,SELECT au_lname,state FROM authors WHERE state NOT IN('CA','IN','MD')



1> tvanfosson..:
    List states = 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 } );



2> CMS..:

你可以用Contains来做:

       var states = new[]  {"CA", "IN", "MD"};
       var query = db.Authors.Where(x => !states.Contains(x.state));



3> Galwegian..:

这是一个例子:

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 );

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有