Click here to Skip to main content
15,913,939 members
Home / Discussions / C#
   

C#

 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Sascha Lefèvre25-Feb-16 5:06
professionalSascha Lefèvre25-Feb-16 5:06 
AnswerRe: Debugging: Stepping into Entity Framework Pin
Dave Kreskowiak25-Feb-16 5:06
mveDave Kreskowiak25-Feb-16 5:06 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Sascha Lefèvre25-Feb-16 5:21
professionalSascha Lefèvre25-Feb-16 5:21 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Dave Kreskowiak25-Feb-16 5:41
mveDave Kreskowiak25-Feb-16 5:41 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Sascha Lefèvre25-Feb-16 6:07
professionalSascha Lefèvre25-Feb-16 6:07 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Sascha Lefèvre25-Feb-16 8:45
professionalSascha Lefèvre25-Feb-16 8:45 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Dave Kreskowiak25-Feb-16 9:25
mveDave Kreskowiak25-Feb-16 9:25 
GeneralRe: Debugging: Stepping into Entity Framework Pin
Sascha Lefèvre25-Feb-16 9:59
professionalSascha Lefèvre25-Feb-16 9:59 
QuestionRx SubscribeOn and ObserveOn Pin
Kenneth Haugland24-Feb-16 20:02
mvaKenneth Haugland24-Feb-16 20:02 
AnswerRe: Rx SubscribeOn and ObserveOn Pin
Pete O'Hanlon25-Feb-16 0:13
mvePete O'Hanlon25-Feb-16 0:13 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 1:42
mvaKenneth Haugland25-Feb-16 1:42 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Pete O'Hanlon25-Feb-16 2:05
mvePete O'Hanlon25-Feb-16 2:05 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 2:31
mvaKenneth Haugland25-Feb-16 2:31 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Pete O'Hanlon25-Feb-16 2:34
mvePete O'Hanlon25-Feb-16 2:34 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 4:38
mvaKenneth Haugland25-Feb-16 4:38 
SuggestionRe: Rx SubscribeOn and ObserveOn Pin
Matt T Heffron25-Feb-16 10:26
professionalMatt T Heffron25-Feb-16 10:26 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 10:33
mvaKenneth Haugland25-Feb-16 10:33 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 20:27
mvaKenneth Haugland25-Feb-16 20:27 
AnswerRe: Rx SubscribeOn and ObserveOn Pin
Matt T Heffron26-Feb-16 7:20
professionalMatt T Heffron26-Feb-16 7:20 
GeneralRe: Rx SubscribeOn and ObserveOn Pin
Kenneth Haugland25-Feb-16 20:55
mvaKenneth Haugland25-Feb-16 20:55 
QuestionIssue with retrieving items from an IEnumerable<T> Pin
RichardGrimmer24-Feb-16 3:27
RichardGrimmer24-Feb-16 3:27 
SuggestionRe: Issue with retrieving items from an IEnumerable<T> Pin
Richard Deeming24-Feb-16 3:50
mveRichard Deeming24-Feb-16 3:50 
Don't measure the performance of your code using DateTime; use the Stopwatch class[^] instead.

Did you make sure to "warm up" the code before you timed it, and run the test over many iterations?

Since you know the maximum number of items you'll be adding to the list, you should set its capacity when you create it:
C#
List<object> entityBatch = new List<object>(batchSize);

If you don't set the capacity, it starts at 4 and doubles each time it runs out of space.

You need to check the value returned from enumerator.MoveNext; if it returns false, you've reached the end of the sequence. Your code currently continues adding the final items from the sequence to the list until you reach the batch size.

You should consider using proper generic types, rather than a List<object>; that way, you avoid having to cast the items back to the correct type when you read the batches.

It's probably best to wrap this sort of thing up as a extension method. Here's the one I use:
C#
public static class EnumerableExtensions
{
    private static IEnumerable<IEnumerable<TSource>> BatchIterator<TSource>(IEnumerable<TSource> source, int size)
    {
        int count = 0;
        var bucket = new TSource[size];
        foreach (var item in source)
        {
            bucket[count] = item;

            checked
            {
                count++;
            }

            if (count == size)
            {
                yield return bucket.Select(x => x);
                bucket = new TSource[size];
                count = 0;
            }
        }
        if (count != 0)
        {
            yield return bucket.Take(count);
        }

        // ReSharper disable once RedundantAssignment
        bucket = null;
    }

    public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(this IEnumerable<TSource> source, int size)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (size < 1) throw new ArgumentOutOfRangeException(nameof(size));
        return BatchIterator(source, size);
    }
}


If you can, try to reproduce the problem in a small example, and either post it here, or create a .NET Fiddle[^] and post the link.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Issue with retrieving items from an IEnumerable<T> Pin
RichardGrimmer24-Feb-16 4:23
RichardGrimmer24-Feb-16 4:23 
GeneralRe: Issue with retrieving items from an IEnumerable<T> Pin
Richard Deeming24-Feb-16 4:34
mveRichard Deeming24-Feb-16 4:34 
GeneralRe: Issue with retrieving items from an IEnumerable<T> Pin
RichardGrimmer24-Feb-16 4:44
RichardGrimmer24-Feb-16 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.