I have three related tables:
Employee(EmployeeId, EmployeeName)
Skill(SkillId, SkillName)
EmployeeSkill(EmployeSkillId, EmployeeId, SkillId)
EmployeSkillId
is an identity.
The rows in the database are the following:
Employee Table:
EmployeeId | EmployeeNumber | EmployeeName ---------- | -------------- | ------------ 1 | 10015 | John Doe
Skill Table:
SkillId | SkillName ------- | --------- 1 | .NET 2 | SQL 3 | OOD 4 | Leadership
EmployeeSkill Table:
EmployeeSkillId | EmployeeId | SkillId --------------- | ---------- | ------- 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 1 | 4
If I have an employee who has three skills registered on EmployeSkill
, I'd like to be able t开发者_JAVA百科o have a result as the following:
John Doe, "Skill-1, Skill2, Skill-3"
That is, to concatenate the name of the skills for that employee into a single string.
I tried the following, but it isn't working.
var query = from emp in Employee.All()
from es in emp.EmployeeSkills
join sk in Skill.All() on es.SkillId equals sk.SkillId
group sk by new {emp.EmployeeName} into g
select new TestEntity
{
Name = g.Key.EmployeeName,
Skills = g.Aggregate(new StringBuilder(),
(sb, grp_row) => sb.Append(grp_row.SkillName))
.ToString()
};
The aggregated list of skill names is coming back empty. How can I do this?
It sounds like you could do the join as part of the select:
var query = from emp in Employee.All()
select new TestEntity {
Name = emp.EmployeeName,
Skills = string.Join(", ",
(from es in emp.EmployeeSkills
join sk in Skill.All() on es.SkillId equals sk.SkillId
select sk.SkillName)) };
Now that's going to do the join separately for each individually, which isn't terribly efficient. Another option is to build a mapping from skill ID to skill name first:
var skillMap = Skill.All().ToDictionary(sk => sk.SkillId,
sk => sk.SkillName);
then the main query is easy:
var query = from emp in Employee.All()
select new TestEntity {
Name = emp.EmployeeName,
Skills = string.Join(", ",
emp.EmployeeSkills.Select(sk => skillMap[sk.SkillId]))};
Ultimately there are lots of ways of skinning this cat - for example, if you wanted to stick to your original approach, that's still feasible. I would do it like this:
var query = from emp in Employee.All()
from es in emp.EmployeeSkills
join sk in Skill.All() on es.SkillId equals sk.SkillId
group sk.SkillName by emp into g
select new TestEntity
{
Name = g.Key.EmployeeName,
Skills = string.Join(", ", g)
};
At this point it's quite similar to your original query, just using string.Join
instead of Aggregate
, of course. If all these three approaches come back with an empty skills list, then I suspect there's something wrong with your data. It's not obvious to me why your first query would "succeed" but with an empty skill list.
EDIT: Okay, here's a short(-ish) but complete example of it working:
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public static List<Employee> All { get; set; }
public IEnumerable<EmployeeSkill> EmployeeSkills
{
get
{
return EmployeeSkill.All
.Where(x => x.EmployeeId == EmployeeId);
}
}
}
public class Skill
{
public string SkillName { get; set; }
public int SkillId { get; set; }
public static List<Skill> All { get; set; }
}
public class EmployeeSkill
{
public int SkillId { get; set; }
public int EmployeeId { get; set; }
public static List<EmployeeSkill> All { get; set; }
}
class Test
{
static void Main()
{
Skill.All = new List<Skill>
{
new Skill { SkillName = "C#", SkillId = 1},
new Skill { SkillName = "Java", SkillId = 2},
new Skill { SkillName = "C++", SkillId = 3},
};
Employee.All = new List<Employee>
{
new Employee { EmployeeName = "Fred", EmployeeId = 1 },
new Employee { EmployeeName = "Ginger", EmployeeId = 2 },
};
EmployeeSkill.All = new List<EmployeeSkill>
{
new EmployeeSkill { SkillId = 1, EmployeeId = 1 },
new EmployeeSkill { SkillId = 2, EmployeeId = 1 },
new EmployeeSkill { SkillId = 2, EmployeeId = 2 },
new EmployeeSkill { SkillId = 3, EmployeeId = 2 },
};
var query = from emp in Employee.All
from es in emp.EmployeeSkills
join sk in Skill.All on es.SkillId equals sk.SkillId
group sk.SkillName by emp.EmployeeName into g
select new
{
Name = g.Key,
Skills = string.Join(", ", g)
};
foreach (var result in query)
{
Console.WriteLine(result);
}
}
}
Results:
{ Name = Fred, Skills = C#, Java }
{ Name = Ginger, Skills = Java, C++ }
精彩评论