2016-11-22 28 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