Linq Inner加入儿童收藏财产

时间:2016-06-24 11:19:01

标签: c# entity-framework linq

我有以下查询转换为Linq

SELECT s.AdvisorName,sg.AdvisorModule,s.StaffID, sg.StaffGroupID, cl.CampusID,sg.Descrip as StaffGroupName,s.LastName
FROM   dbo.Staff s (NOLOCK)
inner join dbo.CampusList cl (NOLOCK) ON cl.CampusGrpID = s.CampusGrpID
inner join dbo.StaffByGroup sbg (NOLOCK) ON s.StaffID = sbg.StaffID
inner join dbo.StaffGroup sg (NOLOCK) ON sg.StaffGroupID = sbg.StaffGroupID
WHERE sg.AdvisorModule = 'NI' AND s.Active = 1 AND cl.CampusID=1

在我的实体模型中,我有以下表格的结构

public partial class Staff
{
    public virtual ICollection<StaffGroupMember> StaffGroupMembership { get; set; }
    public string AdvisorName { get; set; }
    public int StaffID { get; set; }
    public string StaffName { get; set; }
    public string LastName { get; set; }
    public int CampusGrpID { get; set; }
}

public partial class StaffGroupMembers
{
    public virtual StaffGroup StaffGroup { get; set; }
    public int StaffGroupID { get; set; }
    public int StaffID { get; set; }
}

public partial class StaffGroups
{
    public int StaffGroupID { get; set; }
    public string Descrip { get; set; }
    public string AdvisorModule { get; set; }
}

public partial class CampusLists
{
    public int CampusGroupId { get; set; }
    public int CampusId { get; set; }
    public string Campusname { get; set; }
}

我写下面的查询就像sql查询..但我收到错误:MyProj.Models.Staff'不能在LINQ to Entities查询中构造。 我的问题是我们无法指导如果条件对员工对象子属性StaffGroupMembership / StaffGroup ..?以更简单的方式。 请帮忙

var a = (from s in Model.Staff
                     join cl in Model.CampusLists on s.CampusGroupId equals cl.CampusGroupId
                     join sbg in Model.StaffGroupMembers on s.StaffID equals sbg.StaffId
                     join sg in Model.StaffGroups on sbg.StaffGroupId equals sg.StaffGroupID
                     where sg.AdvisorModule == advisorModule && s.IsActive == true && cl.CampusId == campusId
                     select new Staff
                     {
                         StaffName = s.StaffName,
                         StaffID = s.StaffID,
                         DefaultSyCampusId = cl.CampusId,
                         StaffGroupMembership = new List<StaffGroupMembers>()
                         {
                             new StaffGroupMembers()
                             {

                                 StaffGroupId=sg.StaffGroupId,
                                 StaffGroup=new StaffGroup
                                 {
                                     AdvisorModule=sg.AdvisorModule,
                                     Descrip=sg.Descrip
                                 }
                             }
                         }
                     }
                    );

0 个答案:

没有答案