首先,我有一类基本上代表特定项目的所有交易的类,通常我要进行100个项目的交易。该类如下所示:
public class RawTransactions
{
public string SellerName { get; set; }
public int FeedBackScore { get; set; }
public int QuantityPurchased { get; set; }
public DateTime TransactionDate { get; set; }
public string ProductTitle { get; set; }
public double CurrentPrice { get; set; }
public double SalePrice { get; set; }
public string ItemID { get; set; }
public string Date { get; set; }
}
基于此类以及此类列表中的交易,我创建了一个函数,该函数基本上采用列表中存在的任何交易的最小和最大销售价格,并创建7个阶段的价格范围
private List<double> GetRangeForElements(double minPrice, double maxPrice)
{
double step = (maxPrice - minPrice) / 7.00d;
return Enumerable.Range(0, 8).Select(i => minPrice + i * step).ToList();
}
例如,如果我传递$ 0(最小销售价格)和$ 10(最大销售价格),它将创建一个7个价格范围的列表,如下所示:
0
1.5
3
4.5
6
7.5
9
10
这可以解释为:
0 - 1.5 price range
1.5 - 3 price range
3 - 4.5 price range
4.5 - 6 price range
6 - 7.5 price range
// and so on...
用法如下:
var ranges = GetRangeForElements(0,10); // Here I will have the ranges now
现在,基于刚刚创建的这些范围以及我拥有的现有交易,我需要确定以下参数:
我不确定我现在如何才能使用LINQ合并所有这些数据来获取这些参数?有人可以帮我吗?
P.S。伙计们,所有项目的交易都存储在一个列表中,如下所示:
var allItemsTransactions = new List<ProductResearchRawTransactions>();
P.S。伙计们,这是我现有的解决方案,但这给了我完全错误的结果:
var priceRanges = ranges.Select(r => new PriceRangeGraph
{
Price = Math.Round(r, 2),
Sales = allItemsTransactions.Where(x => ranges.FirstOrDefault(y => y >= x.SalePrice) == r).Sum(x => x.QuantityPurchased),
SuccessfulSellers = allItemsTransactions.Where(x => ranges.FirstOrDefault(y => y >= x.SalePrice) == r).GroupBy(x => new { x.SellerName, x.QuantityPurchased }).Where(x => x.Key.QuantityPurchased > 0).Select(x => x.Key.SellerName).Count(),
UnSuccessfulSellers = allItemsTransactions.Where(x => ranges.FirstOrDefault(y => y >= x.SalePrice) == r).GroupBy(x => new { x.SellerName, x.QuantityPurchased }).Where(x => x.Key.QuantityPurchased == 0).Select(x => x.Key.SellerName).Count(),
}).ToList();