日期:2014-05-20  浏览次数:20881 次

求一个 linq 语句!

上图中,主键,外键,引用都已经建立了,

请教如何 用linq 语句 或 函数 一次查询出 如下结构 ,

员工姓名 年龄 业余爱好 专业技能

周锐 22 泡妞,踢球 dotnet , photoshop,3d建模
李强 23 听歌,散步,打豆豆 汽车驾驶,单品机开发


------解决方案--------------------
C# code
 var query = from e in ctx.员工
             let skills = (from s in ctx.员工技能 select new { s.employeeID, s.技能分类.skillName })
             let hobbies = (from h in ctx.员工业余爱好 select new { h.employeeID, h.业余爱好分类.hobbyName })
             select new
             {
                 e.employeeID,
                 e.name,
                 ss = skills.Where(x => x.employeeID == e.employeeID).Select(x => x.skillName),
                 hs = hobbies.Where(x => x.employeeID == e.employeeID).Select(x => x.hobbyName),
             };
 foreach (var q in query)
 {
     Console.WriteLine("{0}\t{1}\t{2}", q.employeeID, string.Join(",", q.ss), string.Join(",", q.hs));
 }

------解决方案--------------------
SQL code

INSERT INTO Employee SELECT 'Tim',30,'China','10086' UNION ALL
SELECT 'Jim',22,'China','10086' UNION ALL
SELECT 'Tom',25,'China','10086' UNION ALL
SELECT 'Jack',38,'China','10086'  

INSERT INTO Skill SELECT 'C#' UNION ALL
SELECT 'java' UNION ALL
SELECT 'JS' UNION ALL
SELECT 'CSS' UNION ALL
SELECT 'LINQ'  

INSERT INTO EmpSkill SELECT 1,1 UNION ALL
 SELECT 1,3 UNION ALL
  SELECT 1,5 UNION ALL
   SELECT 2,2  

INSERT INTO Hobby SELECT '篮球' UNION ALL
SELECT 'dotnet' UNION ALL
SELECT 'photoshop' UNION ALL
SELECT '3d'  

INSERT INTO EmpHobby SELECT 1,1 UNION ALL
SELECT 1,2 UNION ALL
SELECT 2,3 UNION ALL
SELECT 2,4 UNION ALL
SELECT 4,1 UNION ALL
SELECT 4,2

------解决方案--------------------
既能 关系 都建好了, 那 实体类之间就有关系,试着 用关系 看看。
C# code

     var query= from e in Employee
                select new
                {
                   Name=e.Name,
                   Age=e.Age,
                   EmpHobby=string.Join(",",e.EmpHobbies.Select(s=>s.HobbyName)),
                   Skill=string.Join(",",e.EmpSkills.Select(s=>s.SkillName))
                };