Monday, 15 September 2014

c# - .Sum() in lambda expressions -



c# - .Sum() in lambda expressions -

i'm new lambda expressions. i'm trying utilize .sum() method result db search, want sum values importe column, i'm selecting values using id table, json send me entire list every value, it's not doing sum. or maybe don't know how apply it?

thank you

public jsonresult ingresacuentas(string codigo) { contextoangeles db = new contextoangeles(); var suma = (from info in db.cuentas data.codigo == codigo select (from n in db.movimientospolizas data.id == n.idcuenta grouping n new { n.importe} g allow sumatotal = (g.sum(n => n.importe)) select new { total: sumatotal })).tolist(); homecoming json(suma, jsonrequestbehavior.allowget); }

i'm getting in console:

[[{"total":0},{"total":20},{"total":150},{"total":330},{"total":56.2},{"total":240},{"total":1750},{"total":70.07},{"total":480},{"total":540},{"total":95},{"total":200},{"total":108},{"total":108.8},{"total":880},{"total":111.98},{"total":115},{"total":240},{"total":125},{"total":129.98},{"total":780},{"total":131.42},{"total":134.59},{"total":1260},{"total":141.65},{"total":145}]] (and lot more..)

when using select new { total: sumatotal }, that's not sending int, that's sending object of anonymous type total field. don't think that's you're going after.

what think should doing this:

var suma = (from info in db.cuentas data.codigo == codigo select (from n in db.movimientospolizas data.id == n.idcuenta grouping n new { n.importe} g allow sumatotal = (g.sum(n => n.importe)) select sumatotal)).tolist();

or, if you're going select sum of every importe you've queried:

var suma = (from info in db.cuentas data.codigo == codigo select (from n in db.movimientospolizas data.id == n.idcuenta grouping n new { n.importe} g allow sumatotal = (g.sum(n => n.importe)) select sumatotal)).sum();

c# linq lambda linq-expressions

No comments:

Post a Comment