ServiceStack Ormlite SqlExpressionVisitor null check in Where extension -
i write method querying table 1 method null cheking parameters using sqlexpressionvisitor of ormlite here method :
public static list<userchatsdto> getuserchats(int startrow, int rowcount, datetime? startdate, datetime? enddate, string operatorname, short? rating, string visitorname) { using (idbconnection db = dbfactory.opendbconnection()) { sqlexpressionvisitor<userchatsdto> ev = ormliteconfig.dialectprovider.expressionvisitor<userchatsdto>(); ev.where(q => (startdate.hasvalue && q.dated >= startdate) && (enddate.hasvalue && q.dated <= enddate) && (!string.isnullorempty(operatorname) && q.takenbyuser.contains(operatorname)) && (rating.hasvalue && q.rating == (short)rating) && (!string.isnullorempty(visitorname) && q.visitorname.contains(visitorname))); //ev.orderby(); ev.limit(startrow, rowcount); return db.select<userchatsdto>(ev); } }
but object reference not set instance of object. nullreferenceexception thrown when call ev.where part.
is there bug here or missing ? thank you.
you can build expressionvisitor inside select method so:
var chats = db.select<userchatsdto>(q => q .where(x => startdate.hasvalue && x.date >= startdate) .where(x => enddate.hasvalue && x.date <= enddate) .where(x => string.isnullorempty(operatorname) || x.takebyuser.contains(operatorname)) .where(x => rating.hasvalue && x.rating == (short)rating) .where(x => string.isnullorempty(visitorname) || x.visitorname.contains(visitorname) .limit(startrow, rowcount));
Comments
Post a Comment