我正在将多个List聚合到一个List中,并希望根据Foo(Foo.Prop1)的一个属性使其不同。我无权修改Foo的Equality比较器。
Dictionary<string, List<Foo>> fooDictionary = new Dictionary<string, List<Foo>>(); List<Foo> foovals = (from e in fooDictionary where e.Key == "foo1" || e.Key == "foo2" || e.Key == "foo3" select e.Value).SelectMany(f => f).ToList();这里唯一缺少的是最后的.Distinct(),以使Foo独一无二,但是,在我的情况下,我无法修改有关Foo的任何内容,所以简单地调用Distinct()将无效。
有没有办法修改此查询以返回基于Foo.Prop1的项目Distinct?
I am aggregating multiple List's into one List and would like to make it distinct based on one of the properties of Foo (Foo.Prop1).. I do not have access to modify Foo's Equality comparer.
Dictionary<string, List<Foo>> fooDictionary = new Dictionary<string, List<Foo>>(); List<Foo> foovals = (from e in fooDictionary where e.Key == "foo1" || e.Key == "foo2" || e.Key == "foo3" select e.Value).SelectMany(f => f).ToList();the only thing missing here is the .Distinct() at the end, to make Foo's unique, however, in my case, i can't modify anything about Foo, so simply calling Distinct() will not work.
Is there a way to modify this query to return items Distinct based on Foo.Prop1 ?
最满意答案
您可以在MoreLINQ中使用DistinctBy方法:
var query = fooVals.DistinctBy(foo => foo.Property1);You could use the DistinctBy method in MoreLINQ:
var query = fooVals.DistinctBy(foo => foo.Property1);更多推荐
发布评论