I have two tables in my entity data model (converted from an old database)
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
I want to generate a list of last things I ate categorized by the food category. The output looks like:
Categ| Food
-----------
Meat | Pork
Fruit| Orange
Veggi| Tomato
I have the equivalent sql code below, but because we are ditching sql completely, I am trying to achieve the following with entity framework.
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
I was wondering how to achieve this using entity framework?
Try this, assuming your entities are
db.FoodIAteanddb.FoodInFridge(loving the table names!):That way you have the food in order of date eaten, most recent first. Then if you want the top three of them, you can do (make sure you check that the food list is longer/same size as the number you are taking though, otherwise it’ll die!):