日期:2014-05-20 浏览次数:20881 次
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)); }
------解决方案--------------------
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
------解决方案--------------------
既能 关系 都建好了, 那 实体类之间就有关系,试着 用关系 看看。
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)) };