.net - multiple column group by and sum only two columns -


searchlist.groupby(s =>

    new {      s.clientref,     s.clientname,     s.option, s.wineid, s.producer,      s.brand, s.variety, s.format, s.vintage,     s.maturityto, s.rps, s.tradedate, s.target, s.auctionrequested,     s.ukupdated, s.globalupdated })   .select(y => new { clientref = y.key.clientref,  y.key.clientname, y.key.option, y.key.wineid, y.key.producer, y.key.brand, y.key.variety, y.key.format, y.key.vintage,  y.key.maturityto, y.key.rps, y.key.tradedate, y.key.target, y.key.auctionrequested, y.key.ukupdated, y.key.globalupdated,  totalbottles = y.sum(z => z.bottles),   totalunitprice = y.sum(z => z.unit) }).tolist(); 

hi above actual query like. want group key columns come string[] cols rather fix. columns 3,4 or number 18 , sum during group process.

so want searchlist.groupby(s=>cols.split(',')) .select(new {cols.split(','), totalunit= s.sum(z=>z.unit),totalbottles= s.sum(z=>z.bottles)}).tolist ();

step 1: download system.linq.dynamic package or use nuget add project.

step2: try understand code , use it.

               string[] finalcolumnlist = finalgroupbycols.tostring().split(new char[] { ',' }, stringsplitoptions.removeemptyentries);                                     stringbuilder sb = new stringbuilder();                 int counter = 0;                 char com = ',';                 sb.append("new ( ");                 foreach (string colname in finalcolumnlist)                 {                     string right = colname;                     string left = colname;                     if (left == "wineid")                     {                         sb.append("convert.toint32(").append("it[\"").append(left).append("\"] ).tostring() ");                      }                     else if (left == "format" || left == "vintage" || left == "bottles" || left == "maturityto")                     {                         sb.append("convert.todouble(").append("it[\"").append(left).append("\"] ) ");                      }                     else                     {                         sb.append("it[\"").append(left).append("\"] ");                     }                      counter++;                     if (counter == finalcolumnlist.length)                         com = ' ';                     sb.append(right).append(com);                 }                 sb.append(")");                  var sqlval = commonlib.dataconverter.listtodatatableconverter(searchlist).asenumerable().asqueryable().groupby(sb.tostring(), "it")                          .select("new (sum(convert.todouble(it[\"bottles\"].tostring())) totalbottles, sum(convert.todouble(it[\"unit\"].tostring())) totalunit,it.key)").cast<object>().tolist(); ; 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -