score:5
There's nothing within "normal" LINQ to do this directly, but MoreLINQ has a Batch
method which you may find useful:
public static IEnumerable<TResult> Batch<TSource, TResult>
(this IEnumerable<TSource> source, int size,
Func<IEnumerable<TSource>, TResult> resultSelector)
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>
(this IEnumerable<TSource> source, int size)
Note that in your case you'd probably want something like:
var groups = customers.GroupBy(c => c.PartitionKey).Batch(100, p => p.ToList());
so that the returned results are materialized immediately.
Of course, this is assuming you're using LINQ to Objects - if you're trying to partition via another LINQ provider, I'm not sure how you'd go about it.
score:11
For LINQ to Objects:
yourCollection
.Select((v, i) => new {Value = v, Index = i})
.GroupBy(x => x.Index / 100)
Not sure if this works with Azure though...
score:1
This sounds like a job for .Skip
and .Take
, something like the following:
result = collection.Skip(100 * i).Take(100);
Where i
is the page or group number you want to fetch.
score:0
This is my test applying "into" and "take" to group result:
static void Main(string[] args) { int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9,0 }; var result = from n in numbers group n by n%2 into group_numbers select new { short_group = group_numbers.Take(3) }; foreach(var v in result) { foreach (var v1 in v.short_group) { Console.WriteLine(v1.ToString()); } Console.WriteLine(); } }
Output:
1 3 5 2 4 6
Source: stackoverflow.com
Related Articles
- How do I create a Linq expression that creates groups of max 100 objects
- How to create dynamic Linq Select Expression with anonymous objects
- Dynamically creating objects in LINQ expression that inherit from abstract base class
- How can I create a LINQ expression that joins four tables and allows a where on the topmost table?
- c# Linq or code to extract groups from a single list of source data
- Intercepting and overriding a comparison expression in an object that otherwise delegates to Linq to Objects provider
- Saved projection expression for re-use in different linq expressions with two source objects
- I want to create a query that groups by 2 fields and does a count of those combined fields using Linq and C#
- How to write SQL translateable linq code that groups by one property and returns distinct list
- How to flatten nested objects with linq expression
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- How are people unit testing code that uses Linq to SQL
- How to create LINQ Expression Tree to select an anonymous type
- C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?
- Using LINQ to Objects to find items in one collection that do not match another
- How do I create a Linq expression tree with an F# lambda?
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1
- How does a LINQ expression know that Where() comes before Select()?
- Create a Linq Expression with StartsWith, EndsWith and Contains passing a Expression<Func<T, string>>
- Is there a good source that gives an overview of linq optimizations?
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How to reuse a linq expression for 'Where' when using multiple source tables
- Get Value and Count of that value using LINQ or lambda expression
- Linq expression that would always return true
- LINQ query to create a collection of objects combined from a collection of strings
- Can I use LINQ to create a new list of objects from an existing list
- LINQ Source Code Available
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- Include Newest Note and Comment in List
- C# LINQ nullable .Where inline null check
- Join Collection of Objects with LINQ to SQL
- Solution for AND/OR scenario with Linq possible?
- Compare two Iqueryables based on a condition
- LINQ: integer exists in array
- Convert this SQL Count statement to LINQ statement
- Removing Selected Item from List Box
- Distinct and order list LINQ
- Get data from a table after joining based on null value of joined table using LINQ
- LinQ XML mapping to a generic type
- Categorize duplicate elements under same key
- LINQ to SQL grouping error in default VS object visualizer
- generalize Select method of LINQ to Entities in EF5
- Using the "let" kewword in a LINQ Query with EF 4.3
- Get Max() record from table by group
- Querying the nested xml using linq by specifying attribute value
- LINQ query to calculate values from joined collection
- How to select a variable plus some other fields in Linq to object?