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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:12:25+00:00 2026-05-28T15:12:25+00:00

This LINQ and lambda expressions are killing me, so there is me again searching

  • 0

This LINQ and lambda expressions are killing me, so there is me again searching for help here 🙂

What I would like to do is transform List<Order> to List<List<Order>> having few things on mind.

There is some class Order with ID, Currency, and Ammount where basicaly only Currency has some role in this question.

class Order
{
    int ID;
    string Currency;
    money Ammount
}

Beside Order class there is some parameter for the maximum size of list.

int MaxListSize = 3;

And there is the list of orders

List<Order>
{
    1, EUR, 100
    2, EUR, 200
    3, USD, 34
    4, EUR, 12
    5, EUR, 54
    6, USD, 67
    7, EUR, 22
    8, USD, 67
    9, EUR, 89
    10, USD, 64
    11, EUR, 45
    12, USD, 65
    13, USD, 2
    14, EUR, 78
    15, USD, 79
    16, USD, 66
    17, EUR,  3
    18, EUR, 2
}

Transformation should look like this

List<List<Order>>
{
    {1, EUR, 100},  {2, EUR, 200}   {4, EUR, 12}
    {5, EUR, 54},   {7, EUR, 22},   {9, EUR, 89}
    {11, EUR, 45},  {14, EUR, 78},  {17, EUR,  3}
    {18, EUR, 2}
    {3, USD, 34},   {6, USD, 67},   {8, USD, 67}
    {10, USD, 64},  {12, USD, 65},  {13, USD, 2}
    {15, USD, 79},  {16, USD, 66}
}

translated to simple language each element of this output list sholud be list that contains only orders of same currency, and the max size of the element list should be param from the begining, MaxListSize.

And when I’m here, let’s add a little bit more. Lets suppose we have some class OrderGroup which represent one item of output transformation few lines uper, but along with List it has property index.

class OrderGroup
{
    List<Order> OrderList
    int ListIndex
}

output should be

List<OrderGroup>
{
     GroupOrder {OrderList = new List<Order> {{1, EUR, 100},    {2, EUR, 200}   {4, EUR, 12}}, ListIndex  = 1}
     GroupOrder {OrderList = new List<Order> {{5, EUR, 54}, {7, EUR, 22},   {9, EUR, 89}},  ListIndex  = 2}
     GroupOrder {OrderList = new List<Order> {{11, EUR, 45},    {14, EUR, 78},  {17, EUR,  3}}, ListIndex  = 3}
     GroupOrder {OrderList = new List<Order> {{18, EUR, 2}}, ListIndex  = 4}
     GroupOrder {OrderList = new List<Order> {{3, USD, 34}, {6, USD, 67},   {8, USD, 67}}, ListIndex  = 1}
     GroupOrder {OrderList = new List<Order> {{10, USD, 64},    {12, USD, 65},  {13, USD, 2}}, ListIndex  = 2}
     GroupOrder {OrderList = new List<Order> {{15, USD, 79},    {16, USD, 66}}, ListIndex  = 3}
}

Output is basicly same as first, plus there is index, which should depend on Currency. Each currency has it’s own zero based index.

Any help woul be appricieted. I know it should be somehow done with Select() or SelectMany() extension methods, but I don’t know how.

Thanx in advance:)

  • 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-28T15:12:27+00:00Added an answer on May 28, 2026 at 3:12 pm

    This is basically @BrokenGlass’s answer adding a GroupBy and a SelectMany for completeness:

    var results = orders.GroupBy(o => o.Currency)
                        .SelectMany(g => g.Select((order, index) => new { Index = index, Order = order })
                                          .GroupBy(x => x.Index / MaxListSize)
                                          .Select(og => new OrderGroup() 
                                          {
                                             ListIndex = og.Key,
                                             OrderList = og.Select(x => x.Order).ToList()
                                           })
                                          .ToList())
                        .ToList();
    

    To ensure the correct order in general cases, you could add an .OrderBy(o => o.ID) clause at the beginning of the query. But it doesn’t matter in your test example.

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

Sidebar

Related Questions

I'm a newbie with the IQueryable, lambda expressions, and LINQ in general. I would
Please help this Linq newbie! I'm creating a list inside my class under test,
I've been really digging into LINQ, and I'm trying to hash out this lambda
take this linq into consideration: list.Where(sil => sil.XML.Element(ticket) != null && sil.XML.Element(ticket).Attribute(id).Value == smsRequestIn.TicketID)
I need help in turning this linq expression into a linq statment. SampleData.Publishers and
I can't quite figure out why this Linq Statement isn't working as i would
I'm very new to linq and lambda expressions. I'm trying to walk a collection,
So I've just started working with linq as well as using lambda expressions. I've
I have a method which have this signature public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
This is related to my question at Fun (?) with Linq Expressions in extension

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.