2

我有以下课程:

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;}
}

这些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;}
}

我想编写一个linq to entity查询( )来通过它的结果是一个带有s列表的对象Fluent API来查找一个对象,并且每个都包括一个列表 ,我写了以下查询,但它有一些错误:AIdAViewModelBViewModelBViewModelCViewModels

 _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);

我怎样才能做到这一点?

4

1 回答 1

3

尝试用 替换和SelectMany类型Select转换。(ICollection)ToList()

_uow.Repository<A>()
    .All()
    .Select(a => new AViewModel
    {
        Id = a.Id,
        Name = a.Name,
        Bs = a.Bs.Select(b => new BViewModel
        {
            Id = b.Id,
            Name = b.Name,
            Cs = b.Cs.Select(c => new CViewModel
            {
                Id = c.Id,
                Name = c.Name
            }).ToList()
         }).ToList()
    })
    .FirstOrDefault(a => a.Id == Id);
于 2014-09-14T07:56:59.767 回答