不确定如何正确地将数据提取到包含 string、int 和 double 属性的类列表中

我正在使用 asp.net MVC,试图制作一个表格来显示品牌在几天内每天的总利润,并且我正在努力解决如何包含品牌净利润为 0 的日子。


在我查询商店在给定时间范围内的购买总数后,在名为purchaseQuery的变量中,它为我提供了品牌名称、购买日期(作为整数)、该商品的价格以及该商品的数量在那次交易中购买的。假设每个品牌只销售一种产品,并且该产品的价格每天都会波动。该代码位于该查询之后。


purchasedItems = new List<BrandsTransactionItem>();


foreach (var query in purchaseQuery)

{

BrandsTransactionItem tempItem;

tempItem = purchasedItems.Where(e => e.day == query.day).Where(e => e.brandName == query.brandName).FirstOrDefault();


//If item not in list already, 

if (tempItem == null)

{

   purchasedItems.Add(new BrandsTransactionItem()

   {

    brandName = query.brandName,

    day = query.day,

    totalPurchased = query.TotalPurchased,

    costOfItem = query.costOfItem

   });

}

// if it already exists, just add to amount purchased

else

{

  tempItem.totalPurchased += query.totalPurchased;

}

}


foreach (var item in purchasedItems)

{

  double dailyProfit = (double)(item.totalPurchased * item.costOfItem);

  dailyProfit = Math.Round(dailyProfit, 2); // make it decimal of 2


  // Items is the list that I want to display in MVC

  Items.Add(new BrandsProfit(){

  brandName = item.brandName,

  day = item.day,

  totalDailyProfit = dailyProfit

  });

}


我想要进行转换,以便 BrandsProfit 具有名称、日期列表以及与每一天相对应的每日利润列表。这样,商品列表中的每个商品都将具有唯一的品牌名称,因为它将适用于每次购买,而不是仅适用于购买该品牌商品的日期。


我觉得我把事情变得过于复杂了,我主要担心的是,如果一个品牌一天有 0 次购买,我可能会得到一个大小为 5 的天数列表,以及一个大小为 的购买列表4、我需要它们的尺寸相同。


感谢您的帮助,并感谢您花时间阅读我的问题。


偶然的你
浏览 120回答 1
1回答

绝地无双

此类问题非常适合使用 LINQ。下面的 LINQ 查询首先创建不同日期和品牌名称的外连接。这为我们提供了全天可以加入的所有品牌的列表。这样,每个品牌都会有相同的天数,即使它在某一天从未售出任何商品。然后,它会根据该品牌/日期列表加入购买,如果当天没有记录该品牌的购买,则会使用 来创建零成本/数量的替代购买DefaultIfEmpty。然后,该连接的结果将被投影到对象列表中BrandsProfit。例如,如果某一天没有购买某个品牌,您会注意到在示例数据列表中,“2”天的品牌“B”被注释掉了;但在结果中,“2”天仍然存在品牌“B”的行,每日总利润为零。void Main(){&nbsp; &nbsp; var purchases = new List<Purchase>() {&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "A", CostOfItem = 1.13M, Day = 1, TotalPurchased = 125 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "B", CostOfItem = 1.52M, Day = 1, TotalPurchased = 165 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "C", CostOfItem = 1.90M, Day = 1, TotalPurchased = 836 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "A", CostOfItem = 1.74M, Day = 2, TotalPurchased = 583 },&nbsp; &nbsp; &nbsp; &nbsp; //new Purchase() { BrandName = "B", CostOfItem = 1.52M, Day = 2, TotalPurchased = 785 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "C", CostOfItem = 1.42M, Day = 2, TotalPurchased = 369 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "A", CostOfItem = 1.93M, Day = 3, TotalPurchased = 789 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "B", CostOfItem = 1.87M, Day = 3, TotalPurchased = 739 },&nbsp; &nbsp; &nbsp; &nbsp; new Purchase() { BrandName = "C", CostOfItem = 1.78M, Day = 3, TotalPurchased = 436 },&nbsp; &nbsp; };&nbsp; &nbsp; var results = from day in purchases.Select(x => x.Day).Distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from brand in purchases.Select(x => x.BrandName).Distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join purchase in purchases on new { Brand = brand, Day = day } equals new { Brand = purchase.BrandName, Day = purchase.Day } into j&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from result in j.DefaultIfEmpty(new Purchase() { BrandName = brand, Day = day, TotalPurchased = 0, CostOfItem = 0 })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new BrandsProfit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BrandName = result.BrandName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Day = result.Day,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TotalDailyProfit = result.TotalPurchased * result.CostOfItem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; Debug.WriteLine(JsonConvert.SerializeObject(results, Newtonsoft.Json.Formatting.Indented));}class Purchase{&nbsp; &nbsp; public string BrandName { get; set; }&nbsp; &nbsp; public int Day { get; set; }&nbsp; &nbsp; public int TotalPurchased { get; set; }&nbsp; &nbsp; public decimal CostOfItem { get; set; }}class BrandsProfit{&nbsp; &nbsp; public string BrandName { get; set; }&nbsp; &nbsp; public int Day { get; set; }&nbsp; &nbsp; public decimal TotalDailyProfit { get; set; }}产生以下结果:-[&nbsp; {&nbsp; &nbsp; "BrandName": "A",&nbsp; &nbsp; "Day": 1,&nbsp; &nbsp; "TotalDailyProfit": 141.25&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "B",&nbsp; &nbsp; "Day": 1,&nbsp; &nbsp; "TotalDailyProfit": 250.80&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "C",&nbsp; &nbsp; "Day": 1,&nbsp; &nbsp; "TotalDailyProfit": 1588.40&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "A",&nbsp; &nbsp; "Day": 2,&nbsp; &nbsp; "TotalDailyProfit": 1014.42&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "B",&nbsp; &nbsp; "Day": 2,&nbsp; &nbsp; "TotalDailyProfit": 0.0&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "C",&nbsp; &nbsp; "Day": 2,&nbsp; &nbsp; "TotalDailyProfit": 523.98&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "A",&nbsp; &nbsp; "Day": 3,&nbsp; &nbsp; "TotalDailyProfit": 1522.77&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "B",&nbsp; &nbsp; "Day": 3,&nbsp; &nbsp; "TotalDailyProfit": 1381.93&nbsp; },&nbsp; {&nbsp; &nbsp; "BrandName": "C",&nbsp; &nbsp; "Day": 3,&nbsp; &nbsp; "TotalDailyProfit": 776.08&nbsp; }]如果您不想继续使用上面的示例,您可以考虑更新用于生成数据的源查询,以purchaseQuery使用像上面这样的外连接...这样您就会有一个行代表品牌/天的每个组合,即使某个品牌从未在特定日期销售过。
打开App,查看更多内容
随时随地看视频慕课网APP