LINQ:选择对象不包含从列表项 [英] LINQ: Select where object does not contain items from list
问题描述
我在这里LINQ语法挣扎......以为我会折腾出在这里。我不能准确地找到我要找的其他地方。
I'm struggling with LINQ syntax here...thought I'd toss it out here. I cant find exactly what I'm looking for anywhere else.
OK,说我有这样的:
OK, say I've got this:
public class Bar
{
public int BarId { get; set; }
}
public class Foo
{
public List<Bar> BarList { get; set; }
}
List<Foo> fooBunch = GetABunchOfFoos(); //let's just assume I'm getting more than one
List<Foo> fooSelect = new List<Foo>;
List<Bar> filterBars = GetAFewBars(); //assume I'm getting like 2 or 3
fooSelect = (from f in fooBunch
where !(from b in f.BarList select b.BarId).Contains(ITEM FROM filterBars.BarId)
select f).ToList();
所以,长话短说我想使用LINQ过滤掉我的基础上从另一个列表中对象的对象列表。我希望这是有道理的。我想我只是失去了对包含部分......我不知道怎么写了。
So, long story short I want to use LINQ to filter out my list of objects based on objects from another list. I hope this makes sense. I think I'm just lost on the Contains portion...I don't know how to write that.
推荐答案
在一般情况下,你要寻找的除了扩展名。
In general, you're looking for the "Except" extension.
var rejectStatus = GenerateRejectStatuses();
var fullList = GenerateFullList();
var rejectList = fullList.Where(i => rejectStatus.Contains(i.Status));
var filteredList = fullList.Except(rejectList);
在这个例子中,GenerateRegectStatuses()应该是你想拒绝的状态列表(或根据你的榜样,更具体而言,一个列表&LT; INT&GT;
的标识)
In this example, GenerateRegectStatuses() should be the list of statuses you wish to reject (or in more concrete terms based on your example, a List<int>
of IDs)
这篇关于LINQ:选择对象不包含从列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!