LINQ版本的SQL Group By

LINQ version of SQL Group By
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我的SQL设计器目前不可用,我需要完成一些(我认为很简单)的LINQ,我从来没有回过头来学习这些东西,但是我认为这很简单,他甚至还给了我一些SQL代码例如,但是我所有的努力都失败了.

我想做的是这样的:

我有一个列表OrderedItem对象,这些包含ItemName,quantityPPU.

我想从中生成第二个列表,其中仅包含所有这些订单的唯一ItemNames,总计,最大为.quantityItemNamePPU

要清除的是这里剩下的SQL.

Select ItemName
       SUM(Quantity)
       MAX(PPU)
FROM ..... (my list)
Group By ItemName

我会很高兴获得此版本的LINQ.

解决过程1
var query = from rec in dbo.TableName
            group rec by rec.ItemName into ItemNameGroup
            select new { 
                ItemName = ItemNameGroup.Key, 
                SumQuantity = ItemNameGroup.Sum(r => r.Quantity),
                MaxPPU = ItemNameGroup.Max(r => r.PPU)
            };
解决过程2

我认为您的课程是:

class OrderedItem
{
    public string ItemName { get; set; }
    public decimal Quantity { get; set; }
    public int PPU { get; set; }
}

然后,您可以使用扩展方法编写以下LINQ查询:

var objects = new List<OrderedItem>();
objects.GroupBy(x => x.ItemName).Select(x => new
{
    ItemName = x.Key,
    Quantity = x.Sum(y => y.Quantity),
    PPU = x.Max(y => y.PPU)
});
速聊1:
抱歉在这里有点困惑,但是我想我需要先用现有数据填充该新列表?例如new List <OrderedItem>(myList);
速聊2:
要填充列表,您可以使用objects.Add方法.如果您使用的是数据库数据,请阅读有关Entity Framework的信息,并对数据库应用完全相同的查询.

My SQL designer is off currently, and i need to get some (What i assumed was simple) LINQ done, I've never gotten round to learning this stuff, but I thought it would be simple enough, he even left me some SQL code for an example, but all my efforts have failed.

What I'm trying to do is such:

I have a list of OrderedItem Objects, these contain an ItemName, quantity and PPU.

I want to generate a second list from this, with only distinct ItemNames, total for quantity of all those orderditems with that ItemName, and max for PPU.

To clear that up here is the SQL I was left.

Select ItemName
       SUM(Quantity)
       MAX(PPU)
FROM ..... (my list)
Group By ItemName

I would appreciate any help in getting a LINQ version of this.

Solutions1
var query = from rec in dbo.TableName
            group rec by rec.ItemName into ItemNameGroup
            select new { 
                ItemName = ItemNameGroup.Key, 
                SumQuantity = ItemNameGroup.Sum(r => r.Quantity),
                MaxPPU = ItemNameGroup.Max(r => r.PPU)
            };
Solutions2

I assume your class is:

class OrderedItem
{
    public string ItemName { get; set; }
    public decimal Quantity { get; set; }
    public int PPU { get; set; }
}

Then you can write the following LINQ query with extension methods:

var objects = new List<OrderedItem>();
objects.GroupBy(x => x.ItemName).Select(x => new
{
    ItemName = x.Key,
    Quantity = x.Sum(y => y.Quantity),
    PPU = x.Max(y => y.PPU)
});
Talk1:
Sorry to be little dimwitted here, but i assume i would need to fill that new list with my existing data first? e.g new List<OrderedItem>(myList);
Talk2:
To fill the list you can use objects.Add method. If you are using database data then read about Entity Framework and apply the exact same query over the database.
转载于:https://stackoverflow.com/questions/28362535/linq-version-of-sql-group-by

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入