简体   繁体   English

Linq按类别的特定属性分组,然后取最小值和最大值

[英]Linq group by specific property of a class and then take min and max value

I have defined a class like this: 我已经定义了一个这样的类:

 public class GroupedItem
{
    public DateTime _Date { get; set; }
    public int Sales { get; set; }
    public float TransactionPrice { get; set; }

    public string Seller { get; set; }

    // Part used for calculation
    public float BestSellerAveragePrice { get; set; }
    public float MinPrice { get; set; }
    public float MaxPrice { get; set; }
}

And then I have filled it with a list of GroupedItem type like following: 然后,我用GroupedItem类型的列表填充它,如下所示:

List<GroupedItem> _groupedItems = new List<GroupedItems>();

Lets say the list has 300 items whose only initialized values are first four properties like above. 可以说该列表包含300个项目,其唯一的初始化值是上述的前四个属性。 The part with min, max and average price I'm going to fill after a group by statement like following: 具有最小,最大和平均价格的部分将由一组如下语句填充:

ViewBag.PriceAnalytics = _groupedItems.GroupBy(x => x.Seller).Select(gi => new GroupedItem() { MinPrice = x. }).ToList();

As you can see I'm grouping by seller property to get all sales for users which sold something and by the specific price. 如您所见,我正在按卖方属性分组,以按特定价格获得已售出物品的用户的所有销售。

What I'm trying to do is to first sum all these prices that they sold, and then take out an min and max value. 我要尝试做的是先将所有这些价格合计,然后得出最小值和最大值。 So if the input values were: 因此,如果输入值为:

Seller Mike, 21.12.2016, 1 sale, 48$
Seller Mike, 21.12.2016, 1 sale, 24$
Seller Mike, 21.12.2016, 1 sale, 29$


Seller Jenna, 21.12.2016, 1 sale, 20$
Seller Jenna, 21.12.2016, 1 sale, 39$
Seller Jenna, 21.12.2016, 1 sale, 56$

Seller Josh, 21.12.2016, 1 sale, 10$
Seller Josh, 21.12.2016, 1 sale, 28$
Seller Josh, 21.12.2016, 1 sale, 39$

The desired output would be: 所需的输出将是:

Seller Mike, 21.12.2016, 3 sales, 101$
Seller Jenna, 21.12.2016, 3 sales, 115$
Seller Josh, 21.12.2016, 3 sales, 77$

After a first group by.. Now I would have all sellers and their total sales with their total earnings. 经过第一个分组之后。现在,我将拥有所有卖方及其总销售额以及总收入。

Now what I need to do with these 3 values: 现在,我需要使用这三个值:

101, 115, 77 

Min price would be 77$, and max price would be 115$ 最低价格为77 $,最高价格为115 $

How can do this with LINQ? LINQ如何做到这一点?

You could also get a list of summarized data with a LINQ query: 您还可以使用LINQ查询获取汇总数据列表:

var data = (from gp in test
            group gp by gp.Seller into g
            select new GroupedItem
            {
                Seller = g.Key,
                _Date = g.First()._Date,
                Sales = g.Sum(x => x.Sales),
                TransactionPrice = g.Sum(x => x.TransactionPrice)
            }).ToList();
var best = data.Max(x => x.TransactionPrice);
var worst = data.Min(x => x.TransactionPrice);

you are almost there, after grouping on seller, you need to process each group one by one, see : 您已经差不多了,在对卖方分组之后,您需要一个一个地处理每个分组,请参阅:

List<GroupedItem> result = new List<GroupedItem>();
var groupedResult = _groupedItems.GroupBy(x=>x.Seller);

foreach(var grouping in groupedResult)
{

    var row = grouping.First();

    row.TransactionPrice = grouping.Sum(x=>x.TransactionPrice);
    row.Sales = grouping.Sum(x=>x.Sales);

    result.Add(row);
}

DEMO FIDDLE 演示场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM