c# - Is there a better way of shortening this LINQ statement? -
var filtereditemnumber = 0; if (!string.isnullorempty(searchterm)) { filtereditemnumber = this._objectsrep.find( r => r.objecttitle.startswith(searchterm) && r.createddate >= timeperiod.start && r.createddate <= timeperiod.end).count(); } else { filtereditemnumber = this._objectsrep.find(t => t.createddate >= timeperiod.start && t.createddate <= timeperiod.end) .count(); }
i sure there must shorten way rid of if statement cannot figure out how. when use following code filtering returns different result expecting. maybe parentheses not in right place ?
this._objectsrep.find(r => searchterm == null || r.objecttitle.startswith(searchterm) && r.createddate >= timeperiod.start && r.createddate <= timeperiod.end).count()
what trying achieve if serchterm empty or null ignore filter use date range only.
thanks
you don't need list.find
returns new list, can use linq count:
int filtereditemnumber = _objectsrep.count(r => (string.isnullorempty(searchterm) || r.objecttitle.startswith(searchterm)) && r.createddate >= timeperiod.start && r.createddate <= timeperiod.end);
Comments
Post a Comment