8,497
社区成员
发帖
与我相关
我的任务
分享
var result = from x in list
where !(from y in list
where y.id == x.pid
select y).Any()
select x;
之类的。
但是你要先搞明白你是不是要使用 linq to object。如果你是使用其它某种 linq provider,那么要看它支持什么语法,能否原生支持(像这里的Any表达式,如果使用到数据库上,如果不能原生支持,那么你就可能将一个原本10毫秒的查询变为了10秒钟的查询)。不要仅仅抠 linq 这个字儿,还要说明白你是不是 “to object” 或者别的什么 provider。 var res = from p in list
join p1 in list on p.pid equals p1.id into temp_p1
from temp_p2 in temp_p1.DefaultIfEmpty()
where temp_p2 == null
select new { p, temp_p2 };
list.Where(x => list.All(y => y.id != x.pid)).ToList()