Parallel EntityFramework

Is it possible to do some work in parallel with the entity framework for the 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 example, what changes 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 the same what are the differences in the 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 } 
                                                             }); 
} 

      

+2


a source to share


2 answers


No, the query is being executed on the database, not on the client. The database can execute a query using multiple threads to speed up the process, but in any case, you cannot combine server-side processing with parallel client-side extensions.



+8


a source


You can use PLINQ (Parallel LINQ)

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

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



For example (top):

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 foreach, you can also use the TPL (Task Parallel Library) version. http://msdn.microsoft.com/en-us/library/dd460717(v=VS.100).aspx

+1


a source







All Articles