如何聚合连接?
我有三个相关的表:
Employee(EmployeeId, EmployeeName)
Skill(SkillId, SkillName)
EmployeeSkill(EmployeSkillId, EmployeeId, SkillId)
EmployeSkillId
是一个身份。
数据库中的行如下:
员工表:
EmployeeId | EmployeeNumber | EmployeeName ---------- | -------------- | ------------ 1 | 10015 | John Doe
技能表:
SkillId | SkillName ------- | --------- 1 | .NET 2 | SQL 3 | OOD 4 | Leadership
员工技能表:
EmployeeSkillId | EmployeeId | SkillId --------------- | ---------- | ------- 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 1 | 4
如果我有一名员工在 EmployeSkill
上注册了三项技能,我希望能够得到如下结果如下:
John Doe, "Skill-1, Skill2, Skill-3"
即将该员工的技能名称连接成一个字符串。
我尝试了以下操作,但它不起作用。
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()
};
技能名称聚合列表返回为空。我该怎么做?
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 to 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您可以将连接作为选择的一部分进行:
现在将分别为每个单独进行连接,这不是非常有效。另一种选择是首先构建从技能 ID 到技能名称的映射:
然后主要查询很简单:
最终有很多方法可以给这只猫剥皮 - 例如,如果您想坚持原来的方法,那仍然是可行的。我会这样做:
此时,它与您的原始查询非常相似,当然只是使用
string.Join
而不是Aggregate
。如果所有这三种方法返回的技能列表都是空的,那么我怀疑您的数据有问题。对我来说,为什么你的第一个查询会“成功”但技能列表为空,这并不明显。编辑:好的,这是一个简短但完整的示例:
结果:
It sounds like you could do the join as part of the select:
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:
then the main query is easy:
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:
At this point it's quite similar to your original query, just using
string.Join
instead ofAggregate
, 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:
Results: