将Linq中的方法调用到EF

时间:2012-10-30 17:36:42

标签: asp.net-mvc linq-to-entities asp.net-mvc-viewmodel

为了创建ViewModel,我尝试调用方法GetName()来查找UserID的FirstName和LastName,然后将其添加到模型中。但错误告诉“Linq to Entities无法识别方法”。

我如何以另一种方式实现这一目标?

我的代码:

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

1 个答案:

答案 0 :(得分:0)

构建模型后,您需要调用该方法。你可以尝试这样的事情:

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循环执行此操作:

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

对此的缺点是对.ToList的调用将枚举可查询,对数据库执行查询,因此如果您需要在此方法之外进行更多过滤,则可能正在下载不需要的其他数据,造成额外的开销。