2015-10-21 64 views
1

選擇數據我有這些表 ERDLINQ的從3個表

enter image description here

數據庫,我想選擇他們看起來像 Query result

enter image description here

數據

但我不能選擇哪個是姓+名字從Emloyees表在我的查詢的名稱。這是我的代碼

from d in Skills 
where d.SkillID == 5 or d.SkillID == 6 or d.SkillID == 7 
select new 
{ 
    Description = d.Description, 
    Employees = from emp in d.EmployeeSkills 
       select new 
       { 
        Level = emp.Level, 
        YearsExperience =emp.YearsOfExperience 
        Name = from na in emp.Employees 
        select new 
        { 
         na.FirstName + " " + na.LastName 
        } 
       } 
} 
+0

您需要正確的別名/屬性名稱爲:'名稱= na.FirstName +「」 + na.LastName' – Silvermind

+2

具體什麼不與你的代碼的工作?如果你得到一個錯誤是什麼?如果結果不符合你的期望,那麼它們是什麼? – juharr

+0

@Silvermind:我以前嘗試過,但我得到的錯誤消息:「LINQPad.User.EmployeeSkills」不包含關於'僱員的定義和沒有擴展方法「僱員接受型的第一參數「LINQPad.User.EmployeeSkills '可以找到 – Kranatos

回答

0
from d in Skills 
where (d.SkillID == 5 || d.SkillID == 6 || d.SkillID == 7) // or is not supported 
select new 
{ 
    Description = d.Description, 
    Employees = EmployeeSkills 
      .Where(es => es.SkillID == d.SkillID) 
      .Select(es => new 
      { 
       Level = es.Level, 
       YearsExperience = es.YearsOfExperience, 
       Emp = Employees.Single(emp => emp.EmployeeID == es.EmployeeID)      
      }) 
      .Select(es => new 
      { 
       Level = es.Level, 
       YearsExperience = es.YearsExperience, 
       Name = es.Emp.FirstName + " " + es.Emp.LastName))      
      })   
} 
+0

l2s或l2ef查詢中不支持方法。 – Silvermind

+0

@Silvermind不知道,編輯。謝謝 – serdar

+0

@serdar謝謝你的回答。但你的聲明將在僱員表中選擇所有員工的姓名,並顯示在結果,而不是選擇究竟是誰擁有這些3個技能 – Kranatos

1

你必須得到從EmployeeSkills實體提供的參考Employee信息,並使用Select()方法來定義的結果。你可以使用let關鍵字來定義一個子查詢和參考文獻之間導航,樣品9look上查詢)的評論:使用LINQ的流暢API

// define the query 
var query = from d in Skills 

      // define a condition 
      where d.SkillID == 5 or d.SkillID == 6 or d.SkillID == 7 

      // define a value (in this case, a sub-query) 
      let employees = d.EmployeeSkills.Select(x => new { 
            Level = emp.Level, 
            YearsExperience =emp.YearsOfExperience, 
            Name = x.Employee.Name + " " + x.Employee.LastName        
      }) 

      // define a result     
      select new 
      { 
       Description = d.Description,     
       Employees = employees 
      };    
+0

感謝您的幫助。但有什麼辦法可以在C#表達式中做到這一點? – Kranatos

+0

你是什麼意思「C#表達式」? –

+0

我使用LinQPad學習LINQ的。和C#的表達是一個我可以在語言選項卡 – Kranatos

0

var result = Skills.Select(s => new { 
    Description = s.Description, 
    Employees = Employees 
       .Where(e => EmployeeSkills.Where(es => es.SkillID == s.SkillID).Any(es => es.EmployeeID == e.EmployeeID)) 
       .Select(e => new { 
        Name = String.Format("{0} {1}", e.FirstName, e.LastName), 
        Level = EmployeeSkills.Single(es => es.SkillID == s.SkillID && es.EmployeeID == e.EmployeeID).Level, 
        YearsExperience = EmployeeSkills.Single(es => es.SkillID == s.SkillID && es.EmployeeID == e.EmployeeID).YearsOfExperience 
       }) 
});