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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:59+00:00 2026-05-23T06:48:59+00:00

Not sure if the question is titled clearly, but here goes. I have some

  • 0

Not sure if the question is titled clearly, but here goes. I have some rather messy code in my ASP.NET MVC 3 app to wrap every 2 (but make this configurable from 1-n) items in a list in a div tag when they are printed out. I’d like to create a LINQ function like so:

// Can't think of a better name
IEnumerable<V> FormattedSubsetList<T, V>(IEnumerable<T> items, int every = 2, [delegate to a method to join the N elements])
{
}

Sorry if this is kind of complicated… I’ll try an example. Let’s say I have a list of Widget. I want to print out the names of all my widgets (let’s say I have 7) and I want every 2 widgets to be inside a div. So if I have…

var list = new List<Widget>();
list.Add(new Widget() { Name = "One" });
//... you get the picture
list.Add(new Widget() { Name = "Seven" });

IEnumerable<string> newList = FormattedSubsetList<Widget, string>(list, 2, (one, two) => (return "<div>" + string.Join(" ", one.Name, two.Name) + "</div>");
string finalString = string.Join(string.Empty, newList);
// finalString == <div>One Two</div><div>Three Four</div><div>Five Six</div><div>Seven</div>

I apologize if anything is unclear but I simply don’t know what sort of thing this is called and have no idea how to go about implementing it. I know my LINQ syntax is a little off in some places too.

  • 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-23T06:49:00+00:00Added an answer on May 23, 2026 at 6:49 am

    Here are two extension methods that will do exactly what you need. Split method is reusable, it simply splits any IEnumerable into a list of enumerables so each subenumerable has not more than size elements. The second method ToFormattedList is the customer method that does what you asked for.

    public static class Extenstions
    {
        public static IEnumerable<TRestul> ToFormattedList<TElement, TRestul>(
            this IEnumerable<TElement> source,
            int count,
            Func<List<TElement>, TRestul> formatter)
        {
            return source.Split(count).Select(arg => formatter(arg.ToList()));
        }
    
        public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int size)
        {
            var i = 0;
            return
                from element in source
                group element by i++ / size into splitGroups
                select splitGroups.AsEnumerable();
        }
    }
    

    How to use:

    var list = new List<Widget> { new Widget { Name = "One" }, new Widget { Name = "Two" }, new Widget { Name = "Three" }, new Widget { Name = "Four" }, new Widget { Name = "Five" }, new Widget { Name = "Six" }, new Widget { Name = "Seven" }, new Widget { Name = "Eight" } };
    
    var newList = list.ToFormattedList(2, args => "<div>" + args[0].Name + args[1].Name + "</div>");
    var finalString = string.Join(string.Empty, newList);
    // finalString = <div>OneTwo</div><div>ThreeFour</div><div>FiveSix</div><div>SevenEight</div>
    

    However, there will be a problem if list has odd number of elements, because args[1] will throw exception on the last element. So you can do:

    var newList = list.ToFormattedList(2, args => "<div>" + string.Join(" ", args.Select(arg => arg.Name)) + "/<div>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure if this question is already answered. But here it goes:
I'm not sure whether this question belongs on StackOverflow or SuperUser, but here goes
I'm not sure how to title my question, but here goes. I am testing
I'm not sure this question is appropriate here but I hope I could get
Not sure if the title covers the question well, but here it comes. I
I'm not really sure how to title this question but basically I have an
I'm honestly not sure if the title fits the question completely, but here it
Not entirely sure my question title describes what I want to do, but couldn't
I am not sure how clear my question is by the title, but I
I'm not sure if this question fits properly, but I'm wondering in tile-based games,

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.