LinqToSql - Parallel - DataContext and Parallel

In .NET 4 and a multi-core environment, the linq to sql datacontext object uses new parallels if we use DataLoadOptions.LoadWith?

EDIT

I know that linq to sql does not parallelize regular queries. I want to know when we specify DataLoadOption.LoadWith, does it use concurrency to match each object and its entities?

Example:

using(MyDataContext context = new MyDataContext()) { DataLaodOptions options =new DataLoadOptions(); options.LoadWith<Product>(p=>p.Category); return this.DataContext.Products.Where(p=>p.SomeCondition); } 

generates the following sql:

 Select Id,Name from Categories Select Id,Name, CategoryId from Products where p.SomeCondition 

when all products are created, we will have

 categories.ToArray(); Parallel.Foreach(products, p => { p.Category == categories.FirstOrDefault(c => c.Id == p.CategoryId); }); 

or

 categories.ToArray(); foreach(Product product in products) { product.Category = categories.FirstOrDefault(c => c.Id == product.CategoryId); } 

?

+6
parallel-processing linq-to-sql datacontext
source share
1 answer

No, LINQ to SQL does not. On the .NET side, little can be parallelized. All LINQ to SQL does the translation of expression trees into SQL queries. SQL Server will execute these SQL statements and can do this in parallel.

Remember that although you can do something similar with LINQ to SQL LINQ, this is not a good idea:

 // BAD CODE!!! Don't parallelize a LINQ to SQL query var q = from customer in db.Customers.AsParallel() where customer.Id == 5 select customer; 

As long as this gives the right results, you won’t get the productivity boost you are hoping for. PLINQ cannot handle IQueryable objects. Therefore, it will treat IQueryable as IEnumerable (thus iterating). It will process the db.Customers collection db.Customers parallel and use multiple threads to filter clients. Although this sounds good, it means that he will receive a complete set of clients from the database! Without a construct, AsParallel LINQ to SQL will be able to optimize this query by adding WHERE id = @ID to SQL. SQL Server could use indexes (and possibly multiple threads) to retrieve values.

Although there is room for optimization within the LINQ to SQL engine when it comes to matching entities with its entities, there is currently no such optimization (or at least I couldn’t find anyone using a reflector).

+10
source share

All Articles