[英]asparallel plinq vs 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? 我想执行查询AsParallel query.can一些帮助吗?
If you hit the database as db
suggests, then: 如果按db
建议点击数据库,则:
The non-parallel code will be translated to a SQL query and executed by the DBMS directly. 非并行代码将转换为SQL查询并由DBMS直接执行。
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). 特别是如果你有一个CustomerName
的索引,因为它只能扫描那个索引(否则你最终会得到一个全表扫描,但它可能仍然足够快)。
The second one will: 第二个将:
ProductReceives
table. 下载整个ProductReceives
表。 All of it. 所有的。 This will be much slower than the first solution. 这将是比第一个解决方案慢得多 。
You're getting a NullReferenceException
because one of these rows has a NULL
CustomerName
. 您将获得NullReferenceException
因为其中一行具有NULL
CustomerName
。 So you end up calling ((string)null).ToLower()
. 所以你最终调用((string)null).ToLower()
。
The error doesn't occur in the first case because the DMBS will take care of filtering that out by itself. 在第一种情况下不会发生错误,因为DMBS将负责自行过滤。
也许你应该在调用ToLower之前检查客户名是否为null。
db.Tv_ProductReceive.AsParallel().Where(i =>i.CustomerName != null && i.CustomerName.ToLower().Contains(searchTxt)).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.