[英]Removing an empty list of objects from a nested sublist
I have an object, Foo
which contains a List<Bar>
.我有一个对象
Foo
,其中包含一个List<Bar>
。
Bar
contains a List<Baz>
. Bar
包含一个List<Baz>
。
Baz
contains a List<Qux>
. Baz
包含一个List<Qux>
。
Qux
has a property I want to compare. Qux
有一个我想比较的属性。
If I have List<Foo>
, how do I filter out all Foo
objects from the list where List<Qux>
is empty?如果我有
List<Foo>
,如何从List<Qux>
为空的列表中过滤掉所有Foo
对象?
EDIT Updated my question to more appropriately reflect my problem.编辑更新了我的问题以更恰当地反映我的问题。
If you want to keep a Foo
when there exists some Qux
under it with a certain value, do this:如果您想在
Foo
下存在某个具有特定值的Qux
时保留它,请执行以下操作:
List<Foo> foos = ...;
IEnumerable<Foo> query = foos
.Where(foo => foo.Bars
.SelectMany(bar => bar.Bazs)
.SelectMany(baz => baz.Quxs)
.Any(qux => qux.SomeProperty == someValue));
If you want to keep a Foo
when all Qux
under it have a certain value, replace Any
with All
.如果您想在其下的所有
Qux
都具有特定值时保留Foo
,请将Any
替换为All
。
You can also do this with more clear syntax (like for me in that case)你也可以用更清晰的语法来做到这一点(就像我在那种情况下)
List<Foo> lst = GetFooList();
var foos = from foo in lst
from bar in foo.Bars
from baz in bar.Bazs
from qux in baz.Quxs
where qux.Property != somevalue
select foo;
Thanks @31eee384 for putting me on the right track.感谢@31eee384 让我走上正轨。
I also incorporated the Except()
extension to remove the empty lists.我还合并了
Except()
扩展来删除空列表。 I'm sure there is a more efficient method of writing this, so, please post if you know of one.我相信有一种更有效的方法来写这个,所以,如果你知道,请发布。
This is the extension method that I created.这是我创建的扩展方法。
internal static List<Foo> OnlyWithQuxs(this List<Foo> model)
{
var foosToRemove = new List<Foo>();
foreach (var foo in model)
{
var barsToRemove = new List<Bar>();
foreach (var bar in foo.bars)
{
var bazWithQux = new List<Baz>();
var bazsToRemove = new List<Baz>();
foreach (var baz in bar.bazs)
{
baz.Quxs = GetbazQuxs(baz.Id)
.Where(qux => qux.Property == someValue)
.ToList();
bazWithQux.Add(baz);
if (baz.Quxs == null || baz.Quxs.Count() == 0)
bazsToRemove.Add(baz);
}
bar.bazs = bazWithQux.Except(bazsToRemove).ToList();
if (bar.bazs == null || bar.bazs.Count() == 0)
barsToRemove.Add(bar);
}
foo.bars = foo.bars.Except(barsToRemove).ToList();
if (foo.bars == null || foo.bars.Count() == 0)
foosToRemove.Add(foo);
}
return model.Except(foosToRemove).ToList();
}
foos.Where(f=>f.Bars.All(bar=>bar.Bazs.All(baz=>baz.Quxs.Any())>))
This should work where every baz
will not have an empty List<Qux>
这应该适用于每个
baz
不会有空List<Qux>
var foos = new List<Foo>
{
new Foo {Bars = new List<Bar> {new Bar { Bazs = new List<Baz> { new Baz { Quxs = new List<Qux> { new Qux()} } } } } },
new Foo {Bars = new List<Bar> {new Bar { Bazs = new List<Baz> { new Baz { Quxs = new List<Qux> { new Qux()} } } } } },
new Foo {Bars = new List<Bar> {new Bar { Bazs = new List<Baz> { new Baz { Quxs = new List<Qux> { } } } } } },
};
var r = foos.Where(f => f.Bars.All(bars=>bars.Bazs.All(baz=>baz.Quxs.Any()))).ToList();
class Foo
{
public List<Bar> Bars { get; set; }
}
class Bar
{
public List<Baz> Bazs { get; set; }
}
class Baz
{
public List<Qux> Quxs { get; set; }
}
class Qux { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.