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);