mysql - error c# System.NullReferenceException -
i have code:
public ordertotalmodel getordertotalmodelbyagentid(string agentids = null, datetime? fromdate = null, datetime? todate = null) { var session = sessionmanager.currentsession; truncateamount truncateamount = new truncateamount(); string sql = @" select sum(o.totalquantity) totalquantity,sum(o.totalamount) totalamount orders o join workflows w on w.step = o.status inner join agents on a.id = o.agentid w.step = o.status , w.external = true , w.inactive = false , o.agentid in (:agentids) , o.approved = true"; //tu ngay if (fromdate.hasvalue) { sql += " , date(o.created) >= date(:fromdate)"; } // den ngay if (todate.hasvalue) { sql += " , date(o.created) <= date(:todate)"; } var sqlquery = session.createsqlquery(sql) .addscalar("totalquantity", nhibernateutil.double) .addscalar("totalamount", nhibernateutil.double); sqlquery.setstring("agentids", agentids); if (fromdate.hasvalue) { sqlquery.setdatetime("fromdate", (datetime)fromdate); } if (todate.hasvalue) { sqlquery.setdatetime("todate", (datetime)todate); } var result = sqlquery.list<object[]>().select(row => new ordertotalmodel() { totalquantity = (double)row[0], totalamount = truncateamount.truncateamt((double)row[1]), }).firstordefault(); return result; }
when input fromdate # todate return true when input fromdate == todate have error "system.nullreferenceexception". don't know problem, please me.
i think in case fromdate == todate
sql function don't returns values. so, cannot call null.firstordefault()
var result = sqlquery.list<object[]>().select(row => new ordertotalmodel() { totalquantity = (double)row[0], totalamount = truncateamount.truncateamt((double)row[1]), }).firstordefault();
Comments
Post a Comment