1

I have a list of this custom class and I'm trying to find out how to use a linq statement in C# to break that list into groups of 50 items each. Any ideas?

List<Symbol> symbolList = new List<Symbol>(); public class Symbol { public string Symbol { get; set; } public string Market { get; set; } } 
4
  • Not specifically Linq but stackoverflow.com/a/43607422/43846 Commented Aug 13, 2017 at 15:21
  • This might help: github.com/Wee-Projects/LinqExtensions/blob/master/src/… Commented Aug 13, 2017 at 15:29
  • Take and Skip might help. Commented Aug 13, 2017 at 15:29
  • Well, you can use GroupBy but 1: Unfortunatly, you should copy the result to List/Array etc 2: It looks a few difficult for understanding List<int> list = new List<int>(Enumerable.Repeat(2, 249)); int count = 0; int groupNumber = 1; var test = list.GroupBy(item => count++ < 50 ? groupNumber : groupNumber += (count = 1)).ToList(); Commented Aug 13, 2017 at 15:45

1 Answer 1

3

There isn't a built in LINQ method for creating batches, however there's an awesome community maintained project called MoreLinq that contains a .Batch extension as well as all kinds of other helpful extensions.

var batched = symbolList.Batch(50); // returns IEnumerable<IEnumerable<Symbol>> // Where each collection contains 50 items 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.