2016-11-22 27 views
2

我有这样的表的数目:LINQ表达式具有多个与条件联接在每个加入

  1. User_cs
  2. UserEducation_cs
  3. 经验

以下是数据库图表enter image description here

我需要的是用户信息

  1. 他们从User_cs获取的基本信息
  2. 他们上一次接受的教育(即:他们获得的最新学历)
  3. Experience_cs中他们当前正在工作的位置(如果用户不在工作的话)将为NULL。

我到目前为止能够做到以下,但我遇到了麻烦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(); 

任何帮助将不胜感激

+0

您的当前表达式的错误是什么? – slawekwin

+0

它只是在做现在的连接,但我想要的是有条件,我需要检查1)IsWorking in Experience_cs,应该是true 2)在Education_cs中,只有最后一个行应该包含在连接中 – Alex

+0

我认为你必须使用'.GroupJoin()'。在这种情况下,右侧不是单个元素,而是所有匹配元素的IEnumerable。然后,您可以对该序列应用更多条件或过滤器,以获取所需的所需元素(例如,一个,多个或无)。 – Oliver

回答

1

我不能完全肯定,如果我完全理解你的问题。我认为你需要在应用(内部)连接之前过滤你的列表。为了得到每个User_cs只有最新的Education_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(); 
+0

你明白了吗,我也想只选择用户当前正在工作的experience_cs中的行 – Alex

+0

@Alex,是不是“IsWorking”做了什么?如果不是,你如何确定? – slawekwin

+0

由于某种原因UserName = ue.FirstOrDefault()。UserName ...正在工作。你忘了包括FirstOrDefault()? – Alex