【问题标题】:Nested Linq to get Manager detail based on Employee Id嵌套 Linq 以根据员工 ID 获取经理详细信息
【发布时间】:2016-04-24 18:23:05
【问题描述】:

我一直在尝试编写一个 linq 代码,通过传递分配给该特定经理的员工 ID 来获取经理详细信息。我有两张桌子

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

注意:如您所见,第一个表有两个记录,第二个表有报告详细信息,其中 1 个用户向用户 2 报告。所以首先我传递员工 1 id 并获取报告 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 的详细信息。

【问题讨论】:

  • 你在使用实体框架吗?
  • 是的,只需在第一个 linq 查询中添加一个额外的连接
  • 您的问题不清楚,您在参数中传递UserID 并没有使用它?
  • 你能把表UserProfiles的Model结构也放上来吗?

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


【解决方案1】:

如果您使用的是实体框架,那么在您的模型结构中,您必须添加如下虚拟属性-

    //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。这是实体框架的好处。

【讨论】:

  • 是的,我正在使用实体框架,刚刚更新了我上面的表格。您的代码不起作用
【解决方案2】:

我不太确定问题出在哪里,即使没有导航属性,对我来说它看起来也很简单。

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

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

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2017-02-22
    • 2020-11-22
    • 2018-07-31
    • 2016-11-16
    • 2021-09-28
    相关资源
    最近更新 更多