asp.net mvc - Linq query, how to orderby -


i have method gets data database linq queries. data shown expected not in order wan't. need sort products database totalquantity property shown in query 1 , 2. im trying use ordeby i'm not sure how add in context. need one.

this method:

public ilist<bestsellersreportline> dailybestsellersreport() {     orderstatus os;      paymentstatus ps;      shippingstatus ss;     int billingcountryid = 0;     int recordstoreturn = 10;      int orderby = 1;     int groupby = 1;      var range = new     {         starttimeutc = datetime.today.adddays(-1),         endtimeutc = datetime.today.addseconds(-1),         createdonutc = datetime.today.adddays(-1),     };     var query1 = opv in _opvrepository.table                join o in _orderrepository.table on opv.orderid equals o.id                join pv in _productvariantrepository.table on opv.productvariantid equals pv.id                join p in _productrepository.table on pv.productid equals p.id                (o.createdonutc >= range.starttimeutc && o.createdonutc <= range.endtimeutc)                select opv;     var query2 = groupby == 1 ?                //group product variants                opv in query1                group opv opv.productvariantid g                select new                {                    entityid = g.key,                    totalamount = g.sum(x => x.priceexcltax),                    totalquantity = g.sum(x => x.quantity),                }                :                //group products                opv in query1                group opv opv.productvariant.productid g                select new                {                    entityid = g.key,                    totalamount = g.sum(x => x.priceexcltax),                    totalquantity = g.sum(x => x.quantity),                };     switch (orderby)    {       case 1:             {                  query2 = query2.orderbydescending(x => x.totalquantity);             }             break;      case 2:            {                  query2 = query2.orderbydescending(x => x.totalamount);            }            break;      default:            throw new argumentexception("wrong orderby parameter", "orderby");     }      if (recordstoreturn != 0 && recordstoreturn != int.maxvalue)          query2 = query2.take(recordstoreturn);      var result = query2.tolist().select(x =>     {         var reportline = new bestsellersreportline()         {              entityid = x.entityid,              totalamount = x.totalamount,              totalquantity = x.totalquantity         };         return reportline;     }).tolist();      return result; } 

what return

result.orderby(x => x.totalquantity).tolist(); 

update: can think of adding .tolist() end again.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -