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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:22:16+00:00 2026-06-18T14:22:16+00:00

i trying to assign a row number and a Set-number for List, but Set

  • 0

i trying to assign a row number and a Set-number for List, but Set Number containing wrong number of rows in one set.

 var objx = new List<x>();
            var i = 0;
            var r = 1;


            objY.ForEach(x => objx .Add(new x 
                                                  {
                                                      RowNumber = ++i,
                                                      DatabaseID= x.QuestionID,
                                                      SetID= i == 5 ? r++ : i % 5 == 0 ? r += 1 : r
                                                  }));

for Above code like objY Contains 23 rows, and i want to break 23 rows in 5-5 set.
so above code will give the sequence like[Consider only RowNumber]

[1 2 3 4 5][6 7 8 9][ 10 11 12 13 14 ].......

its a valid as by the logic
and if i change the logic for Setid as

 SetID= i % 5 == 0 ? r += 1 : r

Result Will come Like

[1 2 3 4 ][5 6 7 8 9][10 11 12 13 14].

Again correct output of code

but expected for set of 5.

[1 2 3 4 5][ 6  7 8 9 10].........

What i missing………….

i should have taken my Maths class very Serious.

  • 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-18T14:22:17+00:00Added an answer on June 18, 2026 at 2:22 pm

    I think you want something like this:

    var objX = objY.Select((x, i) => new { ObjX = x, Index = i })
        .GroupBy(x => x.Index / 5)
        .Select((g, i) => 
            g.Select(x => new objx
            {
                RowNumber = x.Index + 1
                DatabaseID = x.ObjX.QuestionID,
                SetID = i + 1
            }).ToList())
        .ToList();
    

    Note that i’m grouping by x.Index / 5 to ensure that every group has 5 items.

    Here’s a demo.

    Update

    it will be very helpful,if you can explain your logic

    Where should i start? I’m using Linq methods to select and group the original list to create a new List<List<ObjX>> where every inner list has maximum 5 elements(less in the last if the total-count is not dividable by 5).

    Enumerable.Select enables to project something from the input sequence to create something new. This method is comparable to a variable in a loop. In this case i project an anonymous type with the original object and the index of it in the list(Select has an overload that incorporates the index). I create this anonymous type to simply the query and because i need the index later in the GroupBy“.

    Enumerable.GroupBy enables to group the elements in a sequence by a specified key. This key can be anything which is derivable from the element. Here i’m using the index two build groups of a maximum size of 5:

    .GroupBy(x => x.Index / 5)
    

    That works because integer division in C# (or C) results always in an int, where the remainder is truncated(unlike VB.NET btw), so 3/4 results in 0. You can use this fact to build groups of the specified size.

    Then i use Select on the groups to create the inner lists, again by using the index-overload to be able to set the SetId of the group:

    .Select((g, i) => 
        g.Select(x => new objx
        {
            RowNumber = x.Index + 1
            DatabaseID = x.ObjX.QuestionID,
            SetID = i + 1
        }).ToList())
    

    The last step is using ToList on the IEnumerable<List<ObjX>> to create the final List<List<ObX>>. That also “materializes” the query. Have a look at deferred execution and especially Jon Skeets blog to learn more.

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

Sidebar

Related Questions

I'm trying to list two radio buttons in each row of a table, but
I'm trying to assign integer to a newly created row: INSERT INTO wcategory (name,
I'm trying to select rows one by one in a while loop in SQL
I'm trying to assign ith row values of 2d array to 1d array. Those
I'm trying to create a new row in a table. There are two constraints
I am trying to assign different tooltips to DataGrid rows according to certain conditions.
So i'm trying to assign a hyperlink to a single word in a {p}
I am trying to assign objects in an NSMutableArray using this code - (IBAction)variablePressed:(UIButton
I'm trying to assign DataContext to a MenuItem, which is part of ListBox. <Style
I am currently trying to assign an event handler to dynamically created rectangle objects.

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.