简体   繁体   中英

How to improve the LINQ query?

This is what i got:

int? productID = (ClientProduct != null ? (int?)ClientProduct.ProductID : null);

result = (from po in ((Chase_Media_Pro_Entity_Model)this.NavigationItem.ObjectContext).raPurchaseOrder_List
                  where po.ClientID == Client.CustomerID
                        && ((object.Equals(po.ClientProductID, productID)) || (po.ClientProductID == (productID ?? po.ClientProductID)))
                        && (po.Is_Active == (isActive ?? po.Is_Active))
                        && (po.IsApproved == (isApproved ?? po.IsApproved))
                  orderby po.Is_Active descending, po.IsApproved ascending
                  select po);

Is there a way to improve this line:

&& ((object.Equals(po.ClientProductID, productID)) || (po.ClientProductID == (productID ?? po.ClientProductID)))

This was the only way i could get the right results.

&& (!productID.HasValue || (productID == po.ClientProductID))

How about

&& ((productID.HasValue && po.ClientProductID.HasValue) ? po.ClientProductID.Value = productID.Value : true)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM