Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asparallel plinq vs linq

Tags:

c#

asp.net

linq

I have a code block

var result = db.ProductReceives.Where(x => x.CustomerName.ToLower().Contains(searchTxt)).ToList();

works fine but when i write same query

 var result = db.ProductReceives.AsParallel().Where(x => x.CustomerName.ToLower().Contains(searchTxt)).ToList();

It Shows a error message Object reference not set to an instance of an object.

I want to execute the query AsParallel query.can some one help?

like image 675
syed mhamudul hasan akash Avatar asked Apr 22 '26 18:04

syed mhamudul hasan akash


1 Answers

If you hit the database as db suggests, then:

  • The non-parallel code will be translated to a SQL query and executed by the DBMS directly.
    This is fast. Especially if you have an index on CustomerName as it'll be able to only scan through that index (you'll end up with a full table scan otherwise but it will probably still be fast enough).

  • The second one will:

    • Download the whole ProductReceives table. All of it.
    • It will then create an object for each row.
    • Then it will feed these objects to your parallel check.

    This will be much slower than the first solution.

You're getting a NullReferenceException because one of these rows has a NULL CustomerName. So you end up calling ((string)null).ToLower().

The error doesn't occur in the first case because the DMBS will take care of filtering that out by itself.

like image 118
Lucas Trzesniewski Avatar answered Apr 25 '26 08:04

Lucas Trzesniewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!