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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:40:03+00:00 2026-05-15T19:40:03+00:00

I am trying to improve some code and I can’t think of a better

  • 0

I am trying to improve some code and I can’t think of a better way to do what it currently does. essentially i loop outside for the number of items divided by the number I want to select, then inside that loop I select the item based on an inner loop of the number of items I need to create a single item modifying that by the outerloop value, so in the example below the outer loop loops twice (0,1) and the inner loop twice per outer loop (0,1 then 2,3).

Can anyone see a more readable way to do this or a way to do it without doing the two loops?

Hope that makes sense. Thanks.

Consider XML of

string xml = @"
<MainItem>
 <Select>2</Select>
 <ItemArray>
   <Item>One</Item>
   <Item>Two</Item>
   <Item>Three</Item>
   <Item>Four</Item>
 </ItemArray>
</MainItem>";

var doc = XDocument.Parse(xml);

Here the Select value tells me how many items create one “object” so in this case 2 items are an object (this can be any number but the number of items will always allow for the correct number of objects) currently i am doing somethng like

    List<List<XElement>> items = new List<List<XElement>>();
    for(int i = 0;i < (doc.Descendants("Item").Count() / (int)doc.Element("MainItem").Element("Select"));i++)
    {
      //this is one object
     var singleItem = new List<XElement>();
      for (int j = 0; j <  (int)doc.Element("MainItem").Element("Select"); j++)
     {
         var item =  doc.Descendants("Item").ElementAt(j + (i * (int)doc.Element("MainItem").Element("Select")));
         singleItem.Add(item);
     }
     items.Add(singleItem);
    }

So we end with a list of List with 2 items, each item holds 2 XElements each (One and Two together and Three and Four together)
so..

ItemOne
 One
 Two
ItemTwo
 Three
 Four
  • 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-15T19:40:03+00:00Added an answer on May 15, 2026 at 7:40 pm

    There’s a simple way to do it with a single LINQ statement. You can group by item index / batchSize, as follows:

    int batchSize = (int)doc.Element("MainItem").Element("Select");
    var items = doc
                .Descendants("Item")
                .Select( (e, i) => new {i,e} )
                .GroupBy( g => g.i / batchSize )
                .Select( g => g.Select( gv => gv.e) )
                ;
    

    This gives an IEnumerable<IEnumerable<XElement>>, in the structure you describe.

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

Sidebar

Related Questions

I'm trying to improve some existing code which originally took 3 minutes to prepare
I'm rewriting some code to improve tableview performance. I'm trying to implement the dequeueReusableCellWithIdentifier
I am re-writing some code to improve thread safety. I am trying to pass
I would like to know if some one can improve my code... Using jQuery
I'm trying to write some code to automatically configure a bunch of scheduled tasks
I've written some code that includes a nested loop where the inner loop is
I have a Django query and some Python code that I'm trying to optimize
I am trying to speed up some python code with cython, and I'm making
I need some help about optimisation. I am trying to improve this open-source game
I am trying to do something similar to How can I improve this SQL

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.