需要Linq等效

时间:2012-06-12 12:26:23

标签: vb.net linq sql-to-linq-conversion

帮助在以下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

3 个答案:

答案 0 :(得分: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)
}  

答案 1 :(得分: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是这样的decalerd:

public class Person
{
    public int PersIndex;
    public int Weight;
}

答案 2 :(得分:-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 convertion toolenter image description here

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