在Linq中分组到Sql

时间:2013-05-05 23:08:21

标签: c# sql linq

我正在尝试写linq查询:

SQL:

select s.s_name, sum(sub.evaluation) as suma from submit_task sub
join student s on s.id=sub.student_id
join study_group g on g.id=s.study_group_id
where g.g_name="abcd"
group by s.s_name
order by suma desc

2 个答案:

答案 0 :(得分:3)

看看这篇文章。 http://www.codeproject.com/Articles/35667/How-to-Use-LINQ-GroupBy 1

作者描述的内容与您所要求的内容相近

答案 1 :(得分:0)

var groupName = "abcd";
var query =
    from submitTask in db.submit_task
    join student in db.student
        on submitTask.student_id equals student.id
    join studyGroup in db.study_group
        on student.study_group_id equals studyGroup.id
    where studyGroup.g_name == groupName
    group submitTask by student.s_name into g
    let suma = g.Sum(st => st.evaluation)
    orderby suma descending
    select new
    {
        s_name = g.Key,
        suma = suma,
    };