Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:29:41+00:00 2026-06-04T00:29:41+00:00

I have two tables in my entity data model (converted from an old database)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:29:42+00:00Added an answer on June 4, 2026 at 12:29 am

    Try this, assuming your entities are db.FoodIAte and db.FoodInFridge (loving the table names!):

    var food = (from fif in db.FoodInFridge
               join fia in db.FoodIAte on fif.Food equals fia.Food
               select new
                      {
                          Food = fif.Food,
                          Category = fif.Category,
                          Date = fia.Date
                      })
               .OrderByDescending(f => f.Date)
               .ToList()
    

    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!):

    var topFood = food.Take(3).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that are considered a single entity in my domain model.
I have two tables in my database schema that represent an entity having a
I have a core data model with two tables (meal and ingredients). I am
I have an ADO .Net Entity Data Model for my database. In this Model
I'm using core data and I have a simple two-entity model. My navigation controller
I'm pretty new to MVC 2 using the Entity Framework. I have two tables
My data model contains two tables with composite primary keys and an associative table.
I use c#, linq and EF4. I have two tables in my DataBase represented
I'm simply trying to get data from two sql server db tables using ado.net
In our database model we have a Beneficiary entity. A beneficiary can be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.