c# - Linq Table Group By Get Perecentage in each column -
i have datatable 2 columns:
guitarbrand | status --------------------- fender | sold fender | in stock gibson | in stock gibson | in stock
i want write linq query output
guitarbrand | percentsold | sold/total --------------------------------------- fender | 50% | 1/2 gibson | 100% | 2/2
here's have far:
var groupedtable = b in table.asenumerable() group b b.field<"guitarbrand"> g select new ( guitarbrand = g.key, perecent = (float)g.count()/(float)g.key)
which got post isn't close working, cannot convert string float. i've tried looking @ other posts can't find anything.
thanks!
you can use following (hopefully self explanatory) query:
var groupedtable = b in table.asenumerable() group b b.field<string>("guitarbrand") g let total = g.count() let sold = g.count(e => e.field<string>("status") == "sold") let soldpercent = (float)sold / (float)total select new { guitarbrand = g.key, percentsold = soldpercent.tostring("p"), totalsold = sold + "/" + total };
Comments
Post a Comment