EntityFramework in parallel

c# entity-framework parallel-processing

Question

Is it possible to make some work in parallel with entity framework for following example?

using (var dbContext = new DB())
{
var res = (from c in dbContext.Customers
           orderby c.Name
           select new
                    {
                    c.Id, 
                    c.Name,
                    c.Role
                    }
          ).ToDictionary(c => c.Id,
                         c => new Dictionary<string, object> {
                                                               { "Name",c.Name },
                                                               { "Role", c.Role }
                                                             });
}

For exampe what will be changed if I add AsParrallel?

using (var dbContext = new DB())
{
var res = (from c in dbContext.Customers
           orderby c.Name
           select new
                    {
                    c.Id, 
                    c.Name,
                    c.Role
                    }
          ).AsParallel().ToDictionary(c => c.Id,
                         c => new Dictionary<string, object> {
                                                               { "Name",c.Name },
                                                               { "Role", c.Role }
                                                             });
}

And one more example. The question is same what are the differences in 3 examples.

using (var dbContext = new DB()) 
{ 
var res = (from c in dbContext.Customers.AsParallel() 
           orderby c.Name 
           select new 
                    { 
                    c.Id,  
                    c.Name, 
                    c.Role 
                   } 
          ).AsParallel().ToDictionary(c => c.Id, 
                         c => new Dictionary<string, object> { 
                                                               { "Name",c.Name }, 
                                                               { "Role", c.Role } 
                                                             }); 
} 
1
3
4/28/2010 12:07:50 PM

Accepted Answer

No, the query is run on the database, not in the client. The database may do the query using multiple threads to accelerate the process, but in any case you can't combine server-side processing with client-side Parallel Extensions.

9
4/28/2010 11:18:36 AM

Popular Answer

You can with PLINQ (Parallel LINQ)

http://msdn.microsoft.com/en-us/library/dd460688.aspx

http://msdn.microsoft.com/en-us/magazine/cc163329.aspx

For example (from above):

IEnumerable data = ...; var q = data.AsParallel().Where(x => p(x)).Orderby(x => k(x)).Select(x => f(x));

foreach (var e in q) a(e);

For the foreach you could also look as using the TPL (Task Parallel Library) version. http://msdn.microsoft.com/en-us/library/dd460717(v=VS.100).aspx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow