I have following classes:
public class A
{
public int Id {get;set;}
public string Name {get; set;}
public ICollection<B> Bs {get; set;}
}
public class B
{
public int Id {get;set;}
public string Name {get; set;}
public ICollection<C> Cs {get; set;}
}
public class C
{
public int Id {get;set;}
public string Name {get; set;}
}
and these ViewModels:
public class AViewModel
{
public int Id {get;set;}
public string Name {get; set;}
public ICollection<BViewModel> BViewModels {get; set;}
}
public class BViewModel
{
public string Name {get; set;}
public ICollection<CViewModel> CViewModels {get; set;}
}
public class CViewModel
{
public string Name {get; set;}
}
I want to write a linq to entity query(Fluent API) to find an A object by its Id that result is a AViewModel with a list of BViewModels and each BViewModel include a list of CViewModels too,
I wrote following query but it has some errors:
_uow.Repository<A>()
.All()
.Include("Bs")
.Include("Bs.Cs")
.Select(a => new AViewModel
{
Name = a.Name,
Id = a.Id,
(ICollection<BViewModel>)
a.Bs
.SelectMany(
t => a.Bs
.Select(r => new BViewModel()
{
Name = r.Name
Cs =
(ICollection<CViewModel>)
t.Cs.SelectMany(y => new CViewModel()
{
Name = y.Name
})
}
)
)
}
).FirstOrDefault(a => a.Id == 5);
How can I do that?