将 LINQ 中的值与 GroupBy 结合使用

Pairing up values in LINQ combined with GroupBy

我有一个交易数据列表,我按 ItemID 字段对其进行分组,该字段基本上为我提供了交易发生次数的数据:

  var _transactionsList = TransactionsData.GroupBy(x => x.ItemID).Select(pr => new TransactionsTabResults {
                            ItemID = pr.Key,
                            ItemPrice = pr.Select(x => x.ItemPrice).FirstOrDefault(), // the issue is here, prices are not matched for the exact product...
                            Title = pr.Select(x => x.Title).FirstOrDefault(),
                            TotalSoldItems = pr.Count(),
                            TotalRevenuePerItem = pr.Sum(y => y.ItemPrice),
                            AveragePrice = pr.Average(y => y.ItemPrice),
                            GalleryURL = pr.Select(x => x.GalleryURL).FirstOrDefault()
                        }).ToList();

这里的问题是,在此 LINQ 之后,产品的价格与我预期的不完全匹配。

我已经将它们与 eBay 上的数据进行了比较,价格并没有完全匹配,而是被打乱了,none 与任何...

我该如何解决这个问题?

编辑:这并不是真正与标记的问题重复...

相反,如果我按价格对商品进行分组,我会剩下什么?这不是解决方案...

编辑:这里是一些示例数据

ItemID: 282183606257 AmountPaid: 55.4
ItemID: 282183606257 AmountPaid: 43.5
ItemID: 282183606257 AmountPaid: 36.5

ItemID: 1218218553606234 AmountPaid: 15.4
ItemID: 1218218553606234 AmountPaid: 53.5
ItemID: 1218218553606234 AmountPaid: 66.5

ItemID: 282053079253 AmountPaid: 446.5
ItemID: 282053079253 AmountPaid: 246.5
ItemID: 282053079253 AmountPaid: 346.5

基本上这些是过去 30 天特定卖家在 eBay 上的交易...一件商品可以以不同的价格出售多次(取决于交易的时刻);

我现在怀疑我得到错误结果的原因是因为我按错误的值分组,而这个值实际上并不是唯一的,因此我根本无法为每个项目分配正确的值?

您的问题出在“.FirstOrDefault()”select或您的 select 语句中,在您已经对数据进行分组之后。您将获得每个分组项目的 "semi random" 值。

此修改将为您提供该组的所有聚合数据以及聚合的各个行。

var _transactionsList = TransactionsData.GroupBy(x => x.ItemID).SelectMany(pr =>
 pr.Select(item => new TransactionsTabResults()
 {

    ItemID = pr.Key,
    ItemPrice = item.ItemPrice,
    ItemTitle = item.ItemTitle,
    TotalSoldItems = pr.Count(),
    TotalRevenuePerItem = pr.Sum(y => y.ItemPrice),
    AveragePrice = pr.Average(y => y.ItemPrice),
})).ToList();

首先,我认为您的代码没有任何问题,除了您获得的价格只是产品价格之一。最好有一些选择它的标准(例如最新价格)。

如果您的 Title 和 GalleryURL 没有改变,那么您可以将它们添加到 groupBy。

例如,如果您有日期字段,则以下代码将尝试查找最新价格。

var _transactionsList = TransactionsData
.GroupBy(x => new { x.ItemID, x.Title, x.GalleryURL })
.Select(pr => new TransactionsTabResults
{
    ItemID              = pr.Key.ItemID,
    Title               = pr.Key.Title,
    GalleryURL          = pr.Key.GalleryURL,

    ItemPrice           = pr.OrderByDescending(a=>a.Date).First().ItemPrice ,

    TotalSoldItems      = pr.Count(),
    TotalRevenuePerItem = pr.Sum(y => y.ItemPrice),
    AveragePrice        = pr.Average(y => y.ItemPrice),
}
).ToList();

如果 title 或 Gallery 中的任何一个都可以更改,那么您需要将它们从 groupby 中删除并选择其中一个。

如果你想调试你的代码,你可以 return 你用来显示价格的整行(甚至 return 组中的所有行),例如喜欢

(而不是 return 每个项目的每个结果,您可以为一个 testID 过滤它)

 var debug = TransactionsData
.Where(a=>a.ItemID = testID)
.GroupBy(x => new { x.ItemID, x.Title, x.GalleryURL })
.Select(pr => new  
{
    ItemID              = pr.Key.ItemID,
    Title               = pr.Key.Title,
    GalleryURL          = pr.Key.GalleryURL,

    LatestSale          = pr.OrderByDescending(a=>a.Date).First() ,
    AllSales            = pr.ToList(),

    ItemPrice           = pr.OrderByDescending(a=>a.Date).First().ItemPrice ,

    TotalSoldItems      = pr.Count(),
    TotalRevenuePerItem = pr.Sum(y => y.ItemPrice),
    AveragePrice        = pr.Average(y => y.ItemPrice),
}
).ToList();

然后您可以决定问题是出在您的数据上还是出在您的代码上。