我有个代码块
var result = db.ProductReceives.Where(x => x.CustomerName.ToLower().Contains(searchTxt)).ToList();工作正常,但当我编写相同的查询时
var result = db.ProductReceives.AsParallel().Where(x => x.CustomerName.ToLower().Contains(searchTxt)).ToList();它显示了错误消息对象引用没有设置为对象的实例.。
我想要执行查询AsParallel query.can某个帮助吗?
发布于 2015-10-10 10:26:07
如果按db的建议访问数据库,那么:
CustomerName上有一个索引,因为它只能扫描该索引(否则您将得到一个完整的表扫描,但它可能仍然足够快)。- _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.这将比第一个解决方案慢得多。
您将得到一个NullReferenceException,因为其中一行有一个NULL CustomerName。所以你最后打电话给((string)null).ToLower()。
在第一种情况下,错误不会发生,因为DMBS会自行过滤掉这些错误。
发布于 2016-04-19 09:48:43
可能您应该在调用ToLower之前检查客户名称null或not。
db.Tv_ProductReceive.AsParallel().Where(i =>i.CustomerName != null && i.CustomerName.ToLower().Contains(searchTxt)).ToList();https://stackoverflow.com/questions/33052728
复制相似问题