Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

82
Views
Is there a LINQ way to turn 2 line items into N based on quantity?

Here's code that works, but is there a LINQ way to turn 2 line items into N based on quantity?

var list = new List<LineItem>{
    new LineItem { Info = "two of these", Quantity = 2 },
    new LineItem { Info = "one of these", Quantity = 1 }
};
        
// prints two lines
// 2x - two of these
// 1x - one of these
foreach( var entry in list) Console.WriteLine(entry);

Console.WriteLine("now I want three lines printed as for the shopping bag");
// prints threes lines quantity driving that
// 2x - two of these
// 2x - two of these
// 1x - one of these
        
// this works, but is there a clean LINQy way?
var bag = new List<LineItem>();
foreach( var item in list)
{
    if ( item.Quantity == 1 )
        bag.Add(item);
    else
        for ( var count = 0 ; count < item.Quantity ; ++ count)
            bag.Add(item);
}
foreach( var entry in bag) Console.WriteLine(entry);
11 months ago · Santiago Trujillo
1 answers
Answer question

0

You can use a combination of SelectMany() and Enumerable.Repeat() to achieve what you want here. For example:

var list2 = list.SelectMany(x => Enumerable.Repeat(x, x.Quantity));

  • SelectMany() selects an element from each element x of the input. If any of these elements are sequences themselves, they are all flattened out into a single sequence (rather than say ending up with a list of lists)
  • Enumerable.Repeat() creates a new enumerable based on each item, containing item x repeated x.Quantity times.

Full listing as tested on .NetFiddle: https://dotnetfiddle.net/wpOafs

using System;
using System.Collections.Generic;
using System.Linq;
                    
public class Program
{
    public class LineItem
    {
        public string Info {get;set;}
        public int Quantity {get;set;}
    }
    public static void Main()
    {
        var list = new List<LineItem>{
            new LineItem { Info = "two of these", Quantity = 2 },
            new LineItem { Info = "one of these", Quantity = 1 }
    };
        
        var list2 = list.SelectMany(x => Enumerable.Repeat(x, x.Quantity));
        
        foreach(var item in list2)
        {
            Console.WriteLine(item.Info + item.Quantity);
        }
        
        Console.WriteLine("Hello World");
    }
}
11 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.