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

  • Home
  • SEARCH
  • 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 6552025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:24:34+00:00 2026-05-25T12:24:34+00:00

I have created an asp.net application using Entity Framework. In this I want to

  • 0

I have created an asp.net application using Entity Framework. In this I want to add the records into a list. For this I have to use the foreach loop but it always adding only last record data for all records, meaning it’s showing same data. Here I have pasted my code. Please verify it once and guide where I can change.

     public List<CategoryItems> ListMenuCategory(int MenuId)
    {
        string str = string.Empty;
        string strJSON = string.Empty;
        List<CategoryItems> resultmenu;
        resultmenu = new List<CategoryItems>();
        List<CategoryItems> Result;
        Result = new List<CategoryItems>();
        bool check = true;
        var objmenuCategory = from cat in objEntity.menucategories where cat.MenuId == MenuId && cat.Active == check select cat;
        CategoryItems Categorylist = new CategoryItems();
        foreach (menucategory category in objmenuCategory)
        {
            Categorylist.CategoryName = category.CategoryName;
            Categorylist.Description = category.Description;
            int menuid = category.MenuCategoryId;
            List<menuitem> menuitems = GetMenucategories(menuid);
            foreach (var items in menuitems)
            {
                Categorylist.ItemName = items.ItemName;
                Categorylist.Description = items.Description;
                Categorylist.Price = (float)items.Price;
                string Image = items.Picture;
                Categorylist.Picture = "http://restaurantmanager.testshell.net/Images/" + Image;
                Categorylist.Thumbnail = "http://restaurantmanager.testshell.net/Images/" + items.Thumbnail;
                if (items.CreatedDate != null)
                {
                    Categorylist.CreatedDate = (DateTime)items.CreatedDate;
                }

                if (items.ModifiedDate != null)
                {
                    Categorylist.ModifiedDate = (DateTime)items.ModifiedDate;
                }
                Result.Add(Categorylist);

            }


            // Result.AddRange(menus);


        }
        return Result;



        }


    private List<menuitem> GetMenucategories(int p)
       {
        restaurantEntities objEntity1 = new restaurantEntities();
         var menuitems = from items in objEntity1.menuitems where items.MenuCategoryId == p select items;
   return menuitems.ToList();
     }
  • 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-25T12:24:34+00:00Added an answer on May 25, 2026 at 12:24 pm

    You are creating the Categorylist item outside of the loops, so you are only using one single item, filling it with different data and adding it over and over to the result.

    You have to create the item inside the innermost loop, so that each iteration gets its own object.


    Note: ChrisF also spotted that you call AddRange inside the loop, which has the result that you will add the same set of items over and over. You don’t need to call AddRange at all, you can just skip the Result list entirely and just return resultmenu instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created reports using Crystal Reports in my asp.net application(vb). It was fine.
We have created a web application, using ASP.NET, that allows users to upload documents
I have the following scenario. I have created an ASP.NET web application (framework 3.5)
I have created an asp.net MVC application in VS 2010, and am using TFS.
I hope this makes sense. I have a ASP.NET web application that uses Entity
I have created a new MVC2 project using the ASP.NET MVC2 Web Application, which
I have created my site using ASP.NET Personal Starter Kits 3.5. I use Visual
I am learning Entity Framework 4. I have a simple ASP.NET (Web Form) application.
I'm trying to use Entity Framework in an ASP.NET MVC web application. Let's say
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.

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.