具有多个连接的Linq表达式与每个连接中的条件

时间:2016-11-22 07:25:12

标签: c# .net entity-framework linq

我有很多表格,如:

  1. User_cs
  2. UserEducation_cs
  3. 体验
  4. 以下是数据库图表enter image description here

    我需要的是用户信息

    1. 来自User_cs的基本信息
    2. 他们的最后一次教育(即他们获得的最新学位)
    3. 它们当前正在工作(如果用户不工作),Experience_cs中的字段将为NULL。
    4. 我到目前为止能够做到以下但是我遇到了麻烦1)在lamda表达式中间我是如何做的(IsWorking == true)并且继续加入到下一个表。以下是我的表达

         List<Models.UserInfo> v = context.User_cs
                  .Join(context.UserEducation_cs, u => u.UserName, ue => ue.UserName, (u, ue) => new UserEducation_cs
                  {
                      UserName = ue.UserName,
                      EducationId = ue.EducationId,
                      StartDate = ue.StartDate,
                      EndDate = ue.EndDate
                  }).
                  Join(context.Education_cs, ue => ue.EducationId, e => e.EducationId, (ue, e) => new
                  {
                      UserName = ue.UserName,
                      EducationId = ue.EducationId,
                      StartDate = ue.StartDate,
                      EndDate = ue.EndDate,
                      Title = e.Title,
                      Major = e.Major,
                      MajorDetails = e.MajorDetails,
                      Info = e.Info
                  }).
                  Join(context.Experiences, lst => lst.UserName, ex => ex.UserName, (lst, ex) => new Models.UserInfo
                  {
                      UserName = ex.UserName,
                      EducationId = lst.EducationId,
                      StartDate = lst.StartDate,
                      EndDate = lst.EndDate,
                      Title = lst.Title,
                      Major = lst.Major,
                      MajorDetails = lst.MajorDetails,
                      Info = lst.Info,
                      IsWorking = ex.IsWorking,
                      StartDate_ex=ex.StartedDate,                    
                  }).
                  Where(iw => iw.IsWorking == true).ToList(); 
      

      任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

我不完全确定我是否完全理解你的问题。我认为您需要在将(内部)联接应用于它们之前过滤您的列表。要为每个Education_cs仅获取最新的User_cs条目,我想您可以使用分组。另外,我还没有完全测试过这段代码:

List<Models.UserInfo> v = context.User_cs
        .Join(context.UserEducation_cs, u => u.UserName, ue => ue.UserName, (u, ue) => new UserEducation_cs
        {
            UserName = ue.UserName,
            EducationId = ue.EducationId,
            StartDate = ue.StartDate,
            EndDate = ue.EndDate
        }).
        Join(context.Education_cs.GroupBy(q => q.UserName).Select(q => q.OrderByDescending(w => w.StartDate).First()), 
             ue => ue.EducationId, e => e.EducationId, (ue, e) => new
        {
            UserName = ue.UserName,
            EducationId = ue.EducationId,
            StartDate = ue.StartDate,
            EndDate = ue.EndDate,
            Title = e.Title,
            Major = e.Major,
            MajorDetails = e.MajorDetails,
            Info = e.Info
        }).
        Join(context.Experiences.Where(ex => ex.IsWorking), lst => lst.UserName, ex => ex.UserName, (lst, ex) => new Models.UserInfo
        {
            UserName = ex.UserName,
            EducationId = lst.EducationId,
            StartDate = lst.StartDate,
            EndDate = lst.EndDate,
            Title = lst.Title,
            Major = lst.Major,
            MajorDetails = lst.MajorDetails,
            Info = lst.Info,
            IsWorking = ex.IsWorking,
            StartDate_ex=ex.StartedDate,                    
        }).ToList();