I have the following code using 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
?
Any other suggestions how to optimize perfrormance are welcome..
*** The above sample refers to my custom class that inherits from IEnumerable
. The IsMatch()
method has some conditions inside:
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);
}
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.
TPL should be able to utilize parallelism in Where()
and Min()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.