2
我有我的實體數據模型(從舊數據庫轉換)每種類型的最新記錄?
ENTITY1兩個表:Food_I_Have_In_Fridge:
Food | Category
---------------
Apple | Fruit
Beef | Meat
Tomato| Veggie
Pork | Meat
Bacon | Meat
Orange| Fruit
Carrot| Veggie
ENTITY2:Food_I_ate:
Date | Food
------------
01/01| Apple
01/01| Beef
01/02| Pork
01/03| Orange
01/04| Tomato
我要生成一個列表根據食品類別分類的最後一件事。輸出看起來像:
Categ| Food
-----------
Meat | Pork
Fruit| Orange
Veggi| Tomato
我有以下等價的SQL代碼,但因爲我們是完全拋棄SQL,我試圖實現與實體框架下面。
with FW as (
select a.[Date], f.category, a.food, a.date
from
[Food_I_ate] a
inner join
[Food_I_Have_In_Fridge] f on a.Food = f.Food
)
select
FW2.category,
FW2.Food,
FW2.Date
from
(select FW.category, max(fw.ix) as maxix
from FW group by FW.category) FW
inner join
FW FW2 on FW2.ix = FW.maxix and FW2.category = FW.category
我想知道如何實現這個使用實體框架?
酷感謝您的快速響應!讓我試試看,如果我沒有弄錯,我應該參考linq? – Bonk
確保你的文件中有'使用System.Linq' :) – mattytommo