[英]Using LINQ to Objects to find items in one collection that do not match another
[英]Complex Linq Query find sum of field for n items in another collection
我有一些樣本數據,正在嘗試確定5種“最暢銷產品”,然后嘗試查找2013年6月客戶以ID#1購買的每種產品中有多少種。
我有以下數據結構:
public class MonthlyHistoric
{
public int StoreCode { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public int NumberOfSales { get; set; }
public int ItemCode { get; set; }
}
public class Recipt
{
public int Id { get; set; }
public DateTime SaleDate { get; set; }
public int CustomerID {get;set;}
}
public class ReciptDetails
{
public int Id { get; set; }
public int IdRecipt { get; set; }
public int ItemCode { get; set; }
public int AmountSold { get; set; }
}
我有以下測試數據:
List<MonthlyHistoric> Historic = new List<MonthlyHistoric> {
new MonthlyHistoric() {ItemCode= 101, Month=6, NumberOfSales=20,StoreCode=7,Year= 2013 },
new MonthlyHistoric() {ItemCode= 102, Month=6, NumberOfSales=100,StoreCode=7,Year= 2013 },
new MonthlyHistoric() {ItemCode= 103, Month=6, NumberOfSales=30,StoreCode=7,Year= 2013 },
new MonthlyHistoric() {ItemCode= 105, Month=6, NumberOfSales=20,StoreCode=3,Year= 2013 }};
List<Recipt> Recipts = new List<Recipt> {
new Recipt() { Id = 1, SaleDate = new DateTime(2013, 6, 02), CustomerID =1 },
new Recipt() { Id = 2, SaleDate = new DateTime(2013, 7, 01), CustomerID =1 },
new Recipt() { Id = 3, SaleDate = new DateTime(2013, 6, 04), CustomerID =2 },
new Recipt() { Id = 4, SaleDate = new DateTime(2013, 5, 14), CustomerID =1 },
new Recipt() { Id = 5, SaleDate = new DateTime(2013, 6, 16), CustomerID =2 },
new Recipt() { Id = 6, SaleDate = new DateTime(2013, 6, 12), CustomerID =1 }};
List<ReciptDetails> Details = new List<ReciptDetails> {
new ReciptDetails() { AmountSold = 200, Id = 1, IdRecipt = 1, ItemCode = 101 },
new ReciptDetails() { AmountSold = 234, Id = 1, IdRecipt = 1, ItemCode = 101 },
new ReciptDetails() { AmountSold = 2050, Id = 1, IdRecipt = 2, ItemCode = 101 },
new ReciptDetails() { AmountSold = 20340, Id = 1, IdRecipt = 2, ItemCode = 102 },
new ReciptDetails() { AmountSold = 2300, Id = 1, IdRecipt = 3, ItemCode = 102 },
new ReciptDetails() { AmountSold = 2200, Id = 1, IdRecipt = 3, ItemCode = 103 },
new ReciptDetails() { AmountSold = 200, Id = 1, IdRecipt = 4, ItemCode = 101 },
new ReciptDetails() { AmountSold = 10, Id = 1, IdRecipt = 5, ItemCode = 101 },
new ReciptDetails() { AmountSold = 2400, Id = 1, IdRecipt = 6, ItemCode = 101 }};
使用以下表達式,我可以獲得2013年第6個月商店(在此示例中為#7)銷售的前5名產品
Historic.Where (x => x.StoreCode == 7 && x.Year == 2013 && x.Month== 6).OrderBy (x =>x.NumberOfSales ).Take(5)
現在,對於這5個元素中的每個元素,我需要找出1號客戶在2013年第6個月內購買了多少物品。我必須確定6月發出了哪些收件人,然后檢查相關的詳細信息是否包括最重要的收件人之一5項。
我正在嘗試通過單個Linq表達式來執行此操作。 但是,可以使用多個表達式。
這是帶有第二個LINQ表達式的示例。 我還將您的一句話寫成(IMHO)更易讀的查詢語法。
var topItems = (from h in Historic
where h.StoreCode == 7 && h.Year == 2013 && h.Month == 6
orderby h.NumberOfSales
select h.ItemCode).Take(5).ToList();
var sum =
(from r in Recipts
where r.CustomerID == 1 && r.SaleDate.Year == 2013 && r.SaleDate.Month == 6
join d in Details on r.Id equals d.IdRecipt
where topItems.Contains(d.ItemCode)
select d.AmountSold).Sum();
Console.WriteLine(sum); // 2834
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.