如何提高我的linq查询的性能?

时间:2022-06-01 13:02:47

I have the following code using Linq.

我有以下使用Linq的代码。

 return (from item in this
          where item.IsMatch(orgid, postcode, shipmentMethod, providerCode)
          orderby item.OrderID
          select item.DTime).FirstOrDefault();

For 2 Million records, it needs more than 10 min to return a value. Could someone help me how do I convert this query to one using ParallelEnumerable?

对于2百万条记录,返回值需要10分钟以上。有人可以帮助我如何使用ParallelEnumerable将此查询转换为一个?

Any other suggestions how to optimize perfrormance are welcome..

欢迎任何其他建议如何优化perfmancemance ..

*** The above sample refers to my custom class that inherits from IEnumerable. The IsMatch() method has some conditions inside:

***上面的示例引用了从IEnumerable继承的自定义类。 IsMatch()方法里面有一些条件:

public bool IsMatch(long orgid, string postcode, string shipmentMethod, string providerCode)
{
    if (string.IsNullOrWhiteSpace(providerCode)) providerCode = null;
    if (string.IsNullOrWhiteSpace(shipmentMethod) || shipmentMethod == "0") shipmentMethod = null;
    return (OrgID == 0 || orgid == OrgID) &&
            PostcodeFrom.Length == postcode.Length &&
            string.CompareOrdinal(PostcodeFrom, postcode) <= 0 &&
            string.CompareOrdinal(PostcodeTo, postcode) >= 0 &&
            (ShipmentMethod == null || shipmentMethod == ShipmentMethod) &&                    (ProviderCode == null || providerCode == ProviderCode);
}

1 个解决方案

#1


0  

Try

return this.AsParallel()
    .Where(p=> p.IsMatch(orgid, postcode, shipmentMethod, providerCode))
    .Min(p=> p.OrderID)
    .Select(p=> p.DTime);

As dymanoid mentioned, OrderBy is needless.

正如dymanoid所提到的,OrderBy是不必要的。

TPL should be able to utilize parallelism in Where() and Min()

TPL应该能够在Where()和Min()中使用并行性

#1


0  

Try

return this.AsParallel()
    .Where(p=> p.IsMatch(orgid, postcode, shipmentMethod, providerCode))
    .Min(p=> p.OrderID)
    .Select(p=> p.DTime);

As dymanoid mentioned, OrderBy is needless.

正如dymanoid所提到的,OrderBy是不必要的。

TPL should be able to utilize parallelism in Where() and Min()

TPL应该能够在Where()和Min()中使用并行性