1

帮助在以下 sql 查询中找到 Linq 等效项:

select sum(weight) from (
select weight from pers p
join list l on l.persindex= p.persindex
group by  p.persindex,weight) a
4

3 回答 3

0

我想这就是你需要的:

public int CalcWeight(IEnumerable<Person> pers, IEnumerable<Person> list)
{
    return  
        pers
        .Join(list, p=>p.PersIndex, l=>l.PersIndex, (p, l) => new {p.PersIndex, l.Weight})
        .GroupBy( a => new {a.PersIndex, a.Weight})
        .Sum(group=>group.Key.Weight);
}

数据类 Person 是这样贴花的:

public class Person
{
    public int PersIndex;
    public int Weight;
}
于 2012-06-12T12:49:27.363 回答
0
from p in context.pers  
join l in context.list on l.persindex equals p.persindex 
group by new 
{      p.persindex,
       l.weight 
} into myGroup  
select new()
{      Key = myGroup.Key,
       GroupSum = myGroup.sum(x=>x.weight)
}  
于 2012-06-12T12:34:41.070 回答
-1

VB.NET版

Dim customerList = From p In pers
                   Group Join l In list On 
                     p.persindex Equals l.persindex
                   Into joineddata = Group, 
                        TotalOweight = Sum(p.weight) 
                   Select p.persindex, TotalOweight 

尝试:Linquer SQL to LINQ 转换工具在此处输入图像描述

from p in pers 
joins l in list on  l.persindex equals p.persindex
group by new {p.persindex,l.weight } into grp
select new { sum = grp.sum(x=>x.weight)}
于 2012-06-12T12:30:57.453 回答