我正在努力与LINQ语法在这里…我想我应该把它扔在这里。我在别的地方也找不到我要找的东西。
好的,假设我得到了这个:
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根据另一个列表中的对象过滤出我的对象列表。我希望这是有意义的。我想我只是在包含部分上迷路了…我不知道怎么写
一般情况下,您要寻找的是"Except"扩展名。
var rejectStatus = GenerateRejectStatuses();
var fullList = GenerateFullList();
var rejectList = fullList.Where(i => rejectStatus.Contains(i.Status));
var filteredList = fullList.Except(rejectList);
在本例中,GenerateRegectStatuses()应该是您希望拒绝的状态列表(或者根据您的示例更具体地说,是id的List<int>
)
将其转储到您不想要的id的更具体的集合
var notTheseBarIds = filterBars.Select(fb => fb.BarId);
那么试试这个:
fooSelect = (from f in fooBunch
where !notTheseBarIds.Contains(f.BarId)
select f).ToList();
或:
fooSelect = fooBunch.Where(f => !notTheseBarIds.Contains(f.BarId)).ToList();
试试下面这个简单的LINQ:
//For a file list/array
var files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
//simply use Where ! x.Contains
var notContain = files.Where(x => ! x.Contains(@"$RECYCLE.BIN")).ToList();
//Or use Except()
var containing = files.Where(x => x.Contains(@"$RECYCLE.BIN")).ToList();
notContain = files.Except(containing).ToList();
我没有尝试过,所以我不保证任何东西,但是
foreach Bar f in filterBars
{
search(f)
}
Foo search(Bar b)
{
fooSelect = (from f in fooBunch
where !(from b in f.BarList select b.BarId).Contains(b.ID)
select f).ToList();
return fooSelect;
}