.NET[C#]LINQ如何实现NOT IN的查询?

.NET 作者: Rector 发布时间: 2018-01-13 15:21:52 9阅读 0评论 0收藏 收藏本文

.NET[C#]使用LINQ如何将一个集合划分成多个子集合?

方案一

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

方案二

var answer = list1.Except(list2);

方案三

from item1 in List1
where !(list2.Any(item2 => item2.Email == item1.Email))
select item1;

方案四

var itemIds = inMemoryList.Select(x => x.Id).ToArray();
var otherObjects = context.ItemList.Where(x => !itemIds.Contains(x.Id));

方案五

var NotInRecord =list1.Where(p => !list2.Any(p2 => p2.Email  == p.Email));

方案六

list1.RemoveAll(Item => list2.Contains(Item));

方案七

var secondEmails = (from item in list2
                    select new { Email = item.Email }
                   ).ToList();

var matches = from item in list1
              where !secondEmails.Contains(item.Email)
              select new {Email = item.Email};

转载请注明:图享网 » .NET[C#]LINQ如何实现NOT IN的查询?

分享扩散:

文章评论

获取验证码