[英]Linq to sql : Issues with Join and Group by
I'm having some trouble trying to achieve my "search" query. 我在尝试实现“搜索”查询时遇到了一些麻烦。 I read lots of posts but could not find how to do in my particular case.
我读了很多文章,但找不到适合我的特定情况的方法。 Here is my three tables overview with some values :
这是我的三张表概述,其中包含一些值:
Table Person : 表人:
Id OtherColumns...
1 ...
2 ...
3 ...
Table Job : 表工作:
Id PersonId OtherColumns...
1 1 ...
2 2 ...
3 3 ...
Table Skill : 餐桌技巧:
PersonId Discipline Value OtherColumns...
1 0 1 ...
1 2 2 ...
1 1 3 ...
2 2 4 ...
2 6 5 ...
2 3 6 ...
3 4 7 ...
3 7 8 ...
I would like to have : 我想拥有 :
Person Job TotalValues
1 1 6 (1+2+3)
2 2 15 (4+5+6)
3 3 15 (7+8)
I tried this : 我尝试了这个:
var result =
from job in db.Job
join person in db.Person
on job.PersonId equals person.Id
join skill in db.Skill
on person.Id equals skill.PersonId
group skill by skill.Value into sk
select new
{
Person = ???
Job = ???
TotalValues = sk.Sum(s => s.Value)
};
But don't know how to get back person and job references... "person" and "job" as defined in the join clause don't work. 但是不知道如何找回人和工作的引用……join子句中定义的“人”和“工作”不起作用。 I need those three because I have to perform multiple "where" and "order by" on it later, in my "search" function.
我需要这三个,因为稍后我必须在“搜索”功能中对其执行多个“ where”和“ order by”。
Does it have something to do with sk.Key.Something ?
它与sk.Key.Something有关吗?
If anyone has an idea I would be really, really thankful ! 如果有人有想法,我将非常感谢!
You need to group on multiple columns following way: 您需要按照以下方式对多个列进行分组:
group skill by new
{
skill.Value,
person.PersonId,
job.JobId
} into g
select new
{
Person =g.Key.PersonId,
Job = g.Key.JobId,
TotalValues = g.Sum(x=>x.Value)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.