簡體   English   中英

需要C#查詢中的Linq-一對多表關系,一對多記錄

[英]Linq in C# Query needed - One to many table relationship, one to many records needed

需要幫助才能在linq中完成此任務。

我有3張桌子。

  1. 顧客

  2. 顧客訂單

  3. 訂單詳細信息

我需要在一個查詢中列出所有客戶(僅當他們至少下了一個訂單時),以及每個客戶的所有訂單(僅當訂單價值大於100時)。

問候,

苛刻

這是基於一些假設的猜測:

var result = 
    from customer in Customers.Where(a => a.CustomerOrder.Count > 0)
    from order in CustomerOrder.Where(a => a.OrderDetails.Sum(b => b.Price) > 100)
    select new
    {
        customer, 
        order.OrderDetails
    }

我想說你想要這樣的東西:

from c in Customers
where c.CustomerOrders.Any()
select new
{
  customer,
  Orders = 
    from co in c.CustomerOrders
    where co.TotalPrice > 100
    select co.OrderDetails
}

或者,如果在CustomerOrder表中沒有“ TotalPrice”列,則用以下內容替換內部查詢:

Orders =
  from cp in c.CustomerOrders
  where co.OrderDetails.Sum (od => od.Price) > 100
  select co.OrderDetails

編輯:如果只想包含至少一個訂單且OrderTotalPrice大於100的客戶,請使用let語句:

from c in Customers
let highValueOrders = 
    from co in c.CustomerOrders
    where co.TotalPrice > 100
    select co.OrderDetails
where highValueOrders.Any()
select new
{
  customer,
  highValueOrders
}

您可以類似的方式添加更多條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM