簡體   English   中英

如何在 EF Core 6 中將 SQL 查詢轉換為具有各種條件的 Join on 的 Linq 表達式?

[英]How can I convert a SQL query to a Linq expression for a Join on with various conditions in EF Core 6?

這是我要轉換的 SQL 查詢:

SELECT TOP (10) 
    t1.ProductName, t2.ProductName, 
    COUNT(DISTINCT t1.OrderId) AS num_orders
FROM 
    Reports t1 
JOIN
    Reports t2 ON t1.OrderId = t2.OrderId
               AND t1.ProductId < t2.ProductId
GROUP BY
    t1.ProductName, t2.ProductName
ORDER BY 
    num_orders DESC

如您所見,在“on”中, orderId必須相同,並且其中一個的productId必須小於另一個。

這是我到目前為止所取得的成就(非常不完整):

var reportData = await (from t1 in this.Context.Reports 
                        join t2 in this.Context.Reports 
                             on t1.OrderIdequals t2.OrderId
                        where t1.ProductId < t2.ProductId
                        into GroupedData
                        orderby GroupedData.Key
                        select new 
                        {
                           GroupedData
                        }).ToListAsync();

如果我在“on”中放置一個帶有“and”的表達式並且我嘗試在單獨的“where”中執行它,我會收到錯誤消息,但它仍然不起作用。

另外選擇是不完整的,因為我還沒有設法讓上面的所有代碼工作,所以不要給它任何重要性。

我最接近讓它為我工作的時候我得到了這個人得到的同樣的錯誤: How can I use Linq expression for Join with GroupBy in EF Core 3.1

這是我用來搜索信息的頁面,但它沒有顯示我要查找的內容: https ://learn.microsoft.com/en-us/ef/core/querying/complex-query-operators

我也使用過 Linqer這個 SQL to Linq 存儲庫,但我無法讓它們工作,我是初級:(

有人可以幫助我或推薦我在哪里尋找信息嗎?


編輯:收到幫助后,這是我查詢的最終結果:

var reportData = await (from t1 in this.Context.Reports
                       join t2 in this.Context.Reports on t1.OrderId equals t2.OrderId
                       where t1.ProductId < t2.ProductId
                       group new { t1, t2 } by new { nom1 = t1.ProductName, nom2 = t2.ProductName} into g                          
                       select new
                       {
                         ProductName1 = g.Key.nom1,
                         ProductName2 = g.Key.nom2,
                         NumOrders = g.Select(x => x.t1.OrderId).Distinct().Count()
                       }).ToListAsync();

排序和選擇前 10 個結果不會以這種方式工作,但我會將此查詢轉換為列表並使用 C# 執行此操作。 感謝大家!

試試這個,

var result = (from t1 in Reports
             join t2 in Reports on t1.OrderId equals t2.OrderId
             where t1.ProductId < t2.ProductId
             group new { t1, t2 } by new { t1.ProductName, t2.ProductName } into g
             let numOrders = g.Select(x => x.t1.OrderId).Distinct().Count()
             orderby numOrders descending
             select new
             {
                 ProductName1 = g.Key.ProductName,
                 ProductName2 = g.Key.ProductName2,
                 NumOrders = numOrders
             }).Take(10);

試試這個

           var reportData = (from t1 in Context.Reports
                             join t2 in Context.Reports on t1.OrderId equals t2.OrderId
                             select new { t1 = t1, t2 = t2 }
                 ).Where(x => x.t1.ProductId < x.t2.ProductId)
                  .GroupBy(x => new { t1Id = x.t1.ProductId, t2Id = x.t2.ProductId })
                  .Select(x => new { t1ProductName = x.First().t1.ProductName, x.First().t2.ProductName, num_Orders = x.Select(y => y.t1.OrderId).Distinct().Count()})
                  .OrderByDescending(x => x.num_Orders);

暫無
暫無

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

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