2011-01-17 3 views
0

나는 CompanyViewModel 생성자에서 다음과 같은 모델을 채 웁니다 하나의 LINQ 쿼리를 만들려고 해요 :Linq에 : 여러 목록에 복합 유형으로 캐스트 결과

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. 

W 모자 하나합니까?

답변

1

보기 모델 속성을 IList<T 대신 IEnumerable<T>으로 변경하고 .ToList() 호출을 제거하십시오.

+0

나는 가능성에 대해 생각해 왔으며, 그것은 나의 견해 (나는 ASP.NET MVC 2를 사용하고있다)에 색인으로 항목에 접근하고있다. Model.CompanyUsers [x] .UsersOperations [y] .Permission 기본 모델 바인딩이 작동하도록합니다. – ajbeaven

+1

쿼리를 두 개로 나눌 수 있습니다. 먼저 익명의 타입으로 프로젝트를 만들고, 그 뒤에'.AsEnumerable()'을 실행 한 다음,리스트를 작성하십시오. –