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;
}
GetName()은 무엇을합니까? Linq to Entity 쿼리는 SQL로 변환됩니다. 임의의 메소드를 호출하면 SQL로 변환 할 수 없으므로 실패를 볼 수 있습니다. 그러나이 방법이 데이터베이스에있는 데이터에서만 작동하는 경우 쿼리를 SQL로 변환 할 수있는 방식으로 쿼리를 변경할 수 있으며 계산을 평가하지 않아도 임의의 데이터를 호출 할 수 있습니다 메서드를 호출합니다. – Pawel
@Pawel 그는 GetName()의 정의를 포함했습니다. 아래로 스크롤. –
@KyleTrauberman - 감사합니다. 나는 이것을 놓쳤다. – Pawel