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 4532930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:05:30+00:00 2026-05-21T14:05:30+00:00

So I’ve recently began checking on ASP.NET MVC3 and EFCodeFirst and have gotten stuck.

  • 0

So I’ve recently began checking on ASP.NET MVC3 and EFCodeFirst and have gotten stuck.
I have 2 Models:
Person:

    [Bind(Exclude = "PersonID")]
    public class Person {

        public int PersonID { get; set; }

        [Required(ErrorMessage="Given name is a required field")]
        [DisplayName("Given name")]
        public string GivenName { get; set; }

        [Required(ErrorMessage="Family name is a required field")]
        [DisplayName("family name")]
        public string FamilyName { get; set; }

        [Required(ErrorMessage = "Birthdate is a required field")]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime Birthdate { get; set; }

        [DisplayName("Ingredient")]
        public virtual Ingredient Ingredient { get; set; }
}

and Ingredient:

public class Ingredient {
    public int IngredientID { get; set; }

    [Required(ErrorMessage = "Ingredient is a required field")]
    public string Name { get; set; }

    public virtual ICollection<Person> Persons { get; set; }
}

Now the point is that Ingredient is a semi-fixed table (only business users can add ingredients, but that’s a different story) and a person can input his/her data and his/her favorite ingredient.
This all works fine but I wanted to create a top 10 of ingredients by joining the 2 tables, and then performing a group by, order by, count and limit (to 10 for example).

The issue here is that I’ve tried several things and just can’t get it to work. I’m quite new to LINQ and can’t get my head on how to do this.

Any suggestions would be greatly appreciated!

Edit:
To specify once more, I just need the equivalent of the following query in LINQ:

SELECT i.name, COUNT(p.Ingredient_IngredientID) AS Total
FROM People p, Ingredients i
WHERE p.Ingredient_IngredientID = i.IngredientID
GROUP BY i.name
ORDER BY total DESC;
  • 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-05-21T14:05:31+00:00Added an answer on May 21, 2026 at 2:05 pm

    You should mark the public Ingredient IngredientID { get; set; } as virtual, this way the ingredient table is loaded lazily. Also you should just call it Ingredient since it’s not just the ID, it’s an actual Ingredient that is referenced.

    If i understand you correctly you want to figure out which ingredient is the most popular? If so just group the persons by Ingredient.IngredientID and count the amount of persons in each grouping.

    Another way of doing is to add a

    public virtual ICollection<Person> Persons { get; set;} field to the Ingredient class, now you can just do

    var tenMostPopularIngredients = (from i in Ingredients
                                orderby i.Persons.Count() descending
                                select i).Take(10);
    

    This gives you a dictionary with each ingredient name as key and the count as value, ordered in descending order by count. If you get a ArgumentException it’s because you have multiple ingredients with the same name:

    var tenMostPopularIngredients = (from i in ingredients
                                    let count = i.Persons.Count()
                                    orderby count descending
                                    select new 
                                    {
                                       Ingredient = i, 
                                       Count = count
                                     }
                                     ).Take(10).ToDictionary(t => t.Ingredient.Name,t => t.Count);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.