Linq to entities:将结果转换为多个列出的复杂类型

时间:2011-01-17 21:16:44

标签: linq linq-to-entities

我正在尝试创建一个linq查询,它在CompanyViewModel构造函数中填充以下模型:

public class CompanyViewModel
{
    public IList<CompanyUserViewModel> CompanyUsers { get; set; }
    ...
}

public class CompanyUserViewModel
{
    public User User { get; set; }
    public IList<UserOperationViewModel> UsersOperations { get; set; }
}

public class UserOperationViewModel
{
    public Operation Operation { get; set; }
    public int Permission { get; set; }
}

目前我有以下查询:

return db.Users.Where(u => u.CompanyId == companyId)
               .Select(u => new CompanyUserViewModel { 
                    User = u,
                    UsersOperations = db.UsersInOperations
                        .Where(uo => uo.UserId == uo.UserId)
                        .Select(uo => new UserOperationViewModel{
                             Operation = uo.Operation,
                             Permission = uo.Permission
                        }).ToList()
                }).ToList();

哪个版本,但是当页面运行时我得到

LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[WoodCo.Models.BusinessObject.UserOperationViewModel] ToList[UserOperationViewModel](System.Collections.Generic.IEnumerable`1[WoodCo.Models.BusinessObject.UserOperationViewModel])' method, and this method cannot be translated into a store expression.

一个人做什么?

1 个答案:

答案 0 :(得分:1)

将视图模型属性更改为使用IEnumerable<T>而非IList<T,然后移除.ToList()来电。