I have a list of Objects, where every object has a "Name" and different other stuff. I want to filter out those objects who don't have a unique name in that List.
Is there a LINQ Statement where I can "Union()" all the resulting groups and just return a IEnumerable of my objects?
Like
IEnumerable<MyObject> Results = (from x in Objects
group x by x.Name into g
where g.Count() > 1
select g)
.COMBINE_OR_WHATEVER();
Thanks!
Yes, there is. You can use SelectMany
.
IEnumerable<MyObject> Results = (from x in Objects
group x by x.Name into g
where g.Count() > 1
select g)
.SelectMany(x => x);
I think you want only the object with unique names ("I want to filter out those objects who don't have a unique name in that List"):
var result = Objects.GroupBy(o => o.Name)
.Where(grp => grp.Count() == 1)
.SelectMany(grp => grp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With