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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:07:48+00:00 2026-06-01T22:07:48+00:00

I am working on an MVC project. I need to return an object that

  • 0

I am working on an MVC project. I need to return an object that groups menu items into groups of four to display in the view using foreach iteration. As this is a model being passed to the view, I need to return a non-generic collection class that can be bound on the .cshtml.

My thought is that I could group the list based on some type of count within LINQ to add four new List items to every Dictionary key. However, I haven’t found an expression that would suffice to a Dictionary. I could solve this rather easily by creating another method and iterating again through the collection to assign keys based on a count, but it seems like unneeded iteration and bad practice.

Currently, I am working with the following query.

        var itemsDict = rssDataContext.rss_Application_Blog_Category_Relationships
            .Where(x => x.Application_Blog.rss_Application.ID == 1)
            .Where(x => x.Blog_Category_ID == 1)
            .Select(x => new MenuItem
            {
            Name = x.rss_Application_Blog.Blog_Title,
            Uri = x.rss_Application_Blog.Blog_Uri,
            ID = x.rss_Application_Blog.ID
            });

But is there a way to group this query into groups of 4 class that be populated into Dictionary<int, List<MenuItem>>?

  • 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-01T22:07:49+00:00Added an answer on June 1, 2026 at 10:07 pm

    @JonSkeet’s solution is pretty good. Here’s an alternative implementation using GroupBy to create the sublists and ToDictionary to project them into the final result:

    var itemsDict = rssDataContext.rss_Application_Blog_Category_Relationships
        .Where(x => x.Application_Blog.rss_Application.ID == 1)
        .Where(x => x.Blog_Category_ID == 1)
        .ToArray()
        .Select((x, index) => new
            {
                Index = index,
                Item = new MenuItem
                {
                    Name = x.rss_Application_Blog.Blog_Title,
                    Uri = x.rss_Application_Blog.Blog_Uri,
                    ID = x.rss_Application_Blog.ID
                }
            });
        .GroupBy(i => i.Index / 4)
        .ToDictionary(g => g.Key);
    

    The resulting sequence will look something like this:

    items = new Dictionary<int, IGrouping<int>>()
    {
        0, new[]
        {
            new { Index = 0, Item = new MenuItem { ... } },
            new { Index = 1, Item = new MenuItem { ... } }
        }
        1, new[]
        {
            new { Index = 2, Item = new MenuItem { ... } },
            new { Index = 3, Item = new MenuItem { ... } }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on an ASP.NET MVC project using NHibernate and I need to
I've integrated ASP.NET MVC 3 project into existing Web Forms project. It was working
I'm working in an ASP.NET MVC Beta 1 project, and I've noticed that if
Inspired by the MVC storefront the latest project I'm working on is using extension
In the current MVC project I'm working on, I need to be able to
I am currently working on a ASP.NET MVC 3 project in which I need
I am working on a Scorm MVC project. Basically I need to update a
I'm working on a spring mvc project. I need to change the style of
I am working on a project in ASP.NET MVC using EF4 Code-First for the
So, I'm working on a new project using ASP.NET MVC. Honestly I don't have

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.