简体   繁体   中英

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. I need those three because I have to perform multiple "where" and "order by" on it later, in my "search" function.

Does it have something to do with ? 吗?

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)
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM