1

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
rst
  • 2,510
  • 4
  • 21
  • 47

2 Answers2

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

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

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();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
  • Strange, my `Where(x)` does not allow `.Include` afterwads – rst Nov 10 '17 at 11:43
  • 1
    You need to add a reference to the `System.Data.Entity`. See this: https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx?f=255&MSPPError=-2147217396#Anchor_1 – Roman Koliada Nov 10 '17 at 11:45