2012-06-12 99 views
1

帮助中找到的LINQ等效于以下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 
+5

那你试试这么远吗? –

+0

不要忘记标记为接受,如果你有你想要的信息.. –

+0

我已经更新了我的答案与vb.net版本看看它... –

回答

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

我在vb.net使用此。它与vb.net不同吗? – user1451208

+0

下面是我现在正在使用的查询,它跳过一些值(相同的权重) Double =(从p入行加入l入列表p(“persindex”)等于l(“persindex”)选择CDbl(p(“ “)))Distinct.Sum() 我试过你查询但得到错误TotalOweight =总和(p.weight) – user1451208

+0

@ user1451208 - 现在我混淆了你真正想要的.........没有得到你在所有 –

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

数据类人是decalerd这样的:

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