繁体   English   中英

从列表A中选择属性不在列表B中的项目

[英]Select items from List A where the property is not in List B

我有一个List<Broadcast> ,而Broadcast对象有一个名为Guid的属性。 现在我需要在该列表中找到其Guid属性不是List<Guid>的项目的所有Broadcast对象。 我找到了一个带有Except();的解决方案Except(); 但这对我不起作用。

Broadcasts.Where(x => x.Guid).Except(readBroadcasts);

我究竟做错了什么?

这是一种可以做到的方法:

List<Guid> excludeGuid = ..... // You init guids you want to exclude
List<Broadcast> result = Broadcasts.Where(x => !excludeGuid.Contains(x.Guid)).ToList() ; 

尝试这个 :

Broadcasts.Where(x => !List.Contains(x.Guid))....

您还可以使用List.FindAll

List<Broadcast> notInGuidList = Broadcasts.FindAll(b => !readBroadcasts.Contains(b.Guid));

另一个更冗长但可能更有效的方法是LINQ的“Left-Outer-Join”

var notFound = from bc in Broadcasts
               join guid in readBroadcasts
               on bc.Guid equals guid into gj
               from guidJoin in gj.DefaultIfEmpty()
               where guidJoin == default(Guid)
               select bc;
List<Broadcast> notFoundBroadCasts = notFound.ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM