Get distinct values of List in List

好久不见. 提交于 2020-01-05 06:46:25

问题


I'm struggling with a db application, where I need to get the distinct values. The whole structure looks somewhat like this

class A
{
    public virtual ICollection<B> Bs { get; set; }
}
class B
{
    public virtual C C { get; set; }
}
class C
{
    public int x {get; set;}
}

This is the model from a db and I have a subset of all A's. Now I need to get all distinct values C.x from all those A's. So basically something like

db.A.Where(something).SelectMany(s => s.Bs.SelectMany(t => t.C.x).ToList().Distinct()).ToList()

But it tells me, that the second SelectMany cannot be inferred from the usage.


回答1:


Fix your code like this:

SelectMany(s => s.Bs.Select(t => t.C.x)

Note that B is not a member of A but Bs, and also your second SelectMany should be Select.

Read more https://stackoverflow.com/a/25564528/2946329




回答2:


Could you try this? Also I think it'll be needed to add .Include statements:

db.A.Where(something)/*.Include(a => a.Bs.Select(b => b.C))*/
    .SelectMany(x => x.Bs.Select(b => b.C.x)).Distinct();


来源:https://stackoverflow.com/questions/47221090/get-distinct-values-of-list-in-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!