我正在重寫一段古代代碼,我繼承了這個代碼,我正在查看一個在大型表上執行大量連接的查詢,然後在結果表上執行一系列分組操作來「提取」數據:如何在一次往返中將單個LinQ to SQL表達式的多個結果返回?
INSERT INTO #teh_temp_table
SELECT * FROM teh_large_table
INNER JOIN teh_other_large_table
SELECT customer_id, customer_name FROM * #teh_temp_table
GROUP BY customer_id, customer_name
ORDER BY customer_name
SELECT city_id, city_name FROM * #teh_temp_table
GROUP BY city_id, city_name
ORDER BY city_name
-- repeated n-2 times ...
這個大SQL語句發送到數據庫服務器用ADO.NET SqlCommand和數據返回作爲一個網絡往返ň獨立的結果。
我很難將此轉換爲LinQ to SQL。我一直在試圖做類似於:
from tlt in teh_large_table
join tolt in teh_other_large_table on tlt.pkey equals tolt.fkey into teh_temp_table
from tmp in teh_temp_Table
group tmp by new { tmp.customer_id, tmp.customer_name } into customers
from tmp in teh_temp_table
group tmp by new { tmp.city_id, tmp.city_name } into cities
select new { customers, cities }
但編譯器抱怨。有沒有辦法發出一個等價的LinQ查詢,它不僅可以檢索數據,而且還可以在單次網絡往返中返回它?正如你可以想象的那樣,我不想多次執行那個令人討厭的連接。