嵌套Linq以根据员工ID获取Manager详细信息

时间:2016-01-19 10:16:20

标签: c# mysql linq entity-framework-6

我一直在尝试编写一个linq代码,通过传递分配给该特定Manager的Employee id来获取Manager详细信息。我有2张桌子

//User Table
public UserProfile() //It has both Employee and Manager Records
{
    public long UserProfileID{ get; set; } //Identity Column 
    public string FirstName{ get; set; }

    public virtual ICollection<UserOfficialInfo> UserOfficialInfoes { get; set; }
}

User

//User Official Detail Table
public UserOfficialInfo() //It has Employee to Manager assignment
{
    public long UserOfficialInfoID{ get; set; } //Identity Column
    public long ReportingTo{ get; set; } //This column will store Manager Id for employee
    public long UserProfileID{ get; set; } //Foreign Key to above table

    public virtual UserProfile UserProfile { get; set; }
}

enter image description here

注意:正如您所看到的,第一个表有两个记录,第二个表有报告详细信息,其中用户1向用户2报告。所以首先我传递员工1的身份并获得报告ID,然后我将获得通过从第二表中获取报告ID来从表1中报告人员的详细信息

这是我的代码,我试图使用

public IEnumerable<UserProfile> GetReportingPerson(long UserProfileID)
{
     var user1 =
           from u in DbContext.UserProfiles
           let userofficial = from userss in DbContext.UserProfiles
                              join useof in DbContext.UserOfficialInfoes
                              on userss.UserProfileID equals useof.UserProfileID
                              where useof.UserProfileID == UserProfileID
                              select useof.ReportingTo
           where u.UserProfileID == userofficial.FirstOrDefault()
           select u;
     return user1.ToList();
}

上面的代码给出了结果,但我使用了2个linq代码,是否有任何有效的方法可以一次性获取Manager详细信息。

2 个答案:

答案 0 :(得分:2)

如果您正在使用entityframework,那么在您的模型结构中,您必须添加如下所示的虚拟属性 -

    //User Table
    public class UserDetail //It has both Employee and Manager Records
    {
        public long UserID { get; set; } //Identity Column having both Emp/Manager
        public string Name { get; set; }
        public virtual List<UserOfficialDetail> UserOfficialDetails{ get;set; } //virtual property 
    }

//User Official Detail Table
public class UserOfficialDetail //It has Employee to Manager assignment
{
    public long UserOfficialID { get; set; } //Identity Column
    public long ReportingTo { get; set; } //This column will store Manager Id
    public long UserID { get; set; } //Foreign Key to above table
    public virtual List<UserProfiles> UserProfiles { get;set; } //virtual property
}

然后您可以简单地将您的LinQ查询放入 -

var managerRecords = DbContext.UserDetails.UserOfficialDetails.FirstOrDefault().UserProfiles.ToList();

您甚至不需要匹配ID。这是实体框架的好处。

答案 1 :(得分:0)

我不太确定是什么问题,即使没有导航属性,它对我来说也很简单。

public IEnumerable<UserProfile> GetReportingPerson(long UserProfileID)
{
     var reportingTo =
           from ui in DbContext.UserOfficialInfoes
           join u in DbContext.UserProfiles on ui.ReportingTo equals u.UserProfileID
           where ui.UserProfileID == UserProfileID
           select u;
     return reportingTo.ToList();
}

即。按UserOfficialInfoes过滤UserProfileID,然后通过UserProfile外键获取ReportingTo信息。