2012-10-30 2 views
0

ViewModel을 생성하기 위해 GetName() 메소드를 호출하여 UserID의 FirstName 및 LastName을 찾은 다음 모델에 추가하려고했습니다. 그러나이 오류는 "Linq가 엔티티에서 메서드를 인식하지 못한다"고 알려줍니다.Linq to EF의 메소드 호출

어떻게하면 다른 방식으로이 작업을 수행 할 수 있습니까?

내 코드 :

public IQueryable<SheetList> GetSheetData() 
    { 
     var query = from a in GetSheets() 
        select new SheetList 
        { 
         SheetId = a.ListAllSafetySheets.Id, 
         SheetTitle = a.ListAllSafetySheets.SafetySheetTitle, 
         ProductionManagerId = a.ListAllSafetySheets.ProductionManager, 
         ProductionManagerName = this.GetName(a.ListAllSafetySheets.ProductionManager), 
         ConstructionManagerId = a.ListAllSafetySheets.ConstructionManager, 
         Created = a.ListAllSafetySheets.Created, 
         CreatedBy = a.ListAllSafetySheets.CreatedBy, 
         UserProfile_UserId = a.ListAllUserProfiles.UserId, 
         Project_Id = a.ListAllProjects.Id, 
         ProjectLeaderId = a.ListAllProjects.ProjectLeader, 
         ConstructionLocation_Id = a.ListAllConstructionLocations.Id, 
        }; 
     return query; 
    } 


public IQueryable<DataCollection> GetSheets() 
    { 
     var query = from vSafety in _db.Sheets 
        join vUserProfile in _db.UserProfiles 
        on vSafety.Id 
        equals vUserProfile.UserId 
        join vProject in _db.Projects 
        on vSafety.Id 
        equals vProject.Id 
        join vConstructionLocation in _db.ConstructionLocations 
        on vSafety.Id 
        equals vConstructionLocation.Id 
        orderby vSafety.Created descending 
        select new SafetyAndProjectAndUserAndLocationCollection 
        { 
         ListAllSafetySheets = vSafety, 
         ListAllUserProfiles = vUserProfile, 
         ListAllProjects = vProject, 
         ListAllConstructionLocations = vConstructionLocation 
        }; 
     return query; 
    } 


public string GetName(int? id) 
    { 
     string returnValue; 

     if (id == null) 
     { 
      var userModel = _db.UserProfiles.Single(x => x.UserId == id); 

      string FirstName = userModel.FirstName; 
      string LastName = userModel.LastName; 

      returnValue = FirstName + ", " + LastName; 
     } 
     else 
     { 
      returnValue = ""; 
     } 

     return returnValue; 
    } 
+0

GetName()은 무엇을합니까? Linq to Entity 쿼리는 SQL로 변환됩니다. 임의의 메소드를 호출하면 SQL로 변환 할 수 없으므로 실패를 볼 수 있습니다. 그러나이 방법이 데이터베이스에있는 데이터에서만 작동하는 경우 쿼리를 SQL로 변환 할 수있는 방식으로 쿼리를 변경할 수 있으며 계산을 평가하지 않아도 임의의 데이터를 호출 할 수 있습니다 메서드를 호출합니다. – Pawel

+0

@Pawel 그는 GetName()의 정의를 포함했습니다. 아래로 스크롤. –

+0

@KyleTrauberman - 감사합니다. 나는 이것을 놓쳤다. – Pawel

답변

0

당신이 모델을 구축 한 후 메소드를 호출해야합니다. 당신은 이런 식으로 뭔가를 시도 할 수 있습니다 : 당신이 문제 .ForEach()를 사용하는 데 문제가 있기 때문에

public IQueryable<SheetList> GetSheetData() 
{ 
    var query = from a in GetSheets() 
       select new SheetList 
       { 
        SheetId = a.ListAllSafetySheets.Id, 
        SheetTitle = a.ListAllSafetySheets.SafetySheetTitle, 
        ProductionManagerId = a.ListAllSafetySheets.ProductionManager, 
        ProductionManagerName = a.ListAllSafetySheets.ProductionManager, 
        ConstructionManagerId = a.ListAllSafetySheets.ConstructionManager, 
        Created = a.ListAllSafetySheets.Created, 
        CreatedBy = a.ListAllSafetySheets.CreatedBy, 
        UserProfile_UserId = a.ListAllUserProfiles.UserId, 
        Project_Id = a.ListAllProjects.Id, 
        ProjectLeaderId = a.ListAllProjects.ProjectLeader, 
        ConstructionLocation_Id = a.ListAllConstructionLocations.Id, 
       }; 

    var queryWithNames = query.ToList().ForEach(s => s.ProductionManagerName = this.GetName(s.ProductionManagerName)); 

    return queryWithNames; 
} 

, 정기적 foreach 루프와 함께이 작업을 수행 할 수 있습니다

foreach(var s in query) 
{ 
    s.ProductionManagerName = this.GetName(s.ProductionManagerName); 
} 

이 단점이 전화입니다 toToList는 쿼리 가능한 데이터베이스를 열거하고 데이터베이스에 대해 쿼리를 실행하므로 나중에이 메서드 외부에서 추가 필터를 수행해야하는 경우 필요없는 추가 데이터를 다운로드 할 수 있으므로 추가 오버 헤드가 발생할 수 있습니다.

+0

고마워요! "System.Collections.Generic.List 형식의 인수를 수락하는 'Foreach'와 'Foreach'에 대한 정의가 포함되어 있지 않음 (사용 지시문 또는 어셈블리 참조가 누락 되었습니까?)"오류가 발생합니다. – user1393252

+0

파일의 맨 위에'System.Core.dll'이 참조되어 있고 using System.Collections.Generic이 있는지 확인하십시오. –

+0

예 참조했습니다. 누락 된 참조를 해결할 힌트가 없습니다. 컨텍스트 메뉴에 해결 옵션이 없습니다. – user1393252