问题
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