【问题标题】:Need Linq equivalent需要 Linq 等价物
【发布时间】:2012-06-15 08:00:41
【问题描述】:

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 如果你得到你想要的信息,别忘了把它标记为接受..
  • 我已经用 vb.net 版本更新了我的答案,看看它......

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


【解决方案1】:
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)
}  

【讨论】:

    【解决方案2】:

    我想这就是你需要的:

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

    【讨论】:

      【解决方案3】:

      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 tool

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

      【讨论】:

      • 我在 vb.net 中使用它。 vb.net 有什么不同吗?
      • 下面是我现在使用的查询,它跳过了一些值(相同的权重) Double = (From p In pers Join l In List On p("persindex") Equals l("persindex")选择 CDbl(p("Weight"))).Distinct.Sum() 我试过你的查询,但是 TotalOweight = Sum(p.weight)
      • @user1451208 - 现在我混淆了你真正想要的东西............根本没有得到你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      相关资源
      最近更新 更多