1

I would like to know if there is a way to replace this syncronized enumeration with something that enumerates both database results in parallel?

I've been doing some research, and as far as I can tell I have to create a new instance of the context for each thread, or perhaps even locking the same context instance until both results are completed (if possible?).

What are my options in .NET 4.0 (C# 4.0)?

using (var context = new DbContext())
{
    IEnumerable<T1> dfd1 = context.T1;
    IEnumerable<T2> dfd2 = context.T2;

    var result1 = dfd1.ToList();
    var result2 = dfd2.ToList();
}
Johan
  • 35,120
  • 54
  • 178
  • 293

2 Answers2

1

Just execute ToList() on two threads, for example using Task.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

This probably won't work as is, but something like this may help you get on the way...

public static class Extensions
{
    public static async Task<List<T>> AsyncToList<T>(this IEnumerable<T> enumerable)
    {
        return enumerable.ToList();
    }
}

Usage:

var result = await enumerable.AsyncToList();
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • Ah sorry - you could always use the [BCL](http://blogs.msdn.com/b/bclteam/archive/2012/10/22/using-async-await-without-net-framework-4-5.aspx) to target.NET 4.0 – dav_i Nov 28 '13 at 13:55