c# - How to do optional parameters in LINQ? -


here i'm trying do:

(dc.det_cases.where(c => c.productid == pl.productid                      && oldtor == false ? c.oldtor == oldtor :                      && (productlinename.toint() == 0 || productlinename.toint() == c.productlineid)                     && (productcategory.toint() == 0 || productcategory.toint() == c.productcategoryid)                     && (issuetype.toint() == 0 || issuetype.toint() == c.issuetypeid)                     && (issue.toint() == 0 || issue.toint() == c.issueid)                     )                     .firstordefault() != null) 

this line i'm trying do.

oldtor == false ? c.oldtor == oldtor : 

inside where linq statement. if value false compare value. if not, ignore it.

the easiest way set other option true.

ie: !oldtor ? c.oldtor == oldtor : true

this means if oldtor false, want compare oldtor's, otherwise, keep evaluating rest of expression.

as aside, split each part of massive .where() boolean comparison individual .where() statements. improve readability , comprehension of linq.

    dc.det_cases         .where(c => c.productid == pl.productid)         .where(c => !oldtor ? c.oldtor == oldtor :  true)         .where(c => productlinename.toint() == 0 || productlinename.toint() == c.productlineid)         .where(c => productcategory.toint() == 0 || productcategory.toint() == c.productcategoryid)         .where(c => issuetype.toint() == 0 || issuetype.toint() == c.issuetypeid)         .where(c => issue.toint() == 0 || issue.toint() == c.issueid)         .firstordefault() != null; 

this makes clear if oldtor false want compare, otherwise, pass statement (true).


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -