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 }
});
}
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.
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