Monday, 15 February 2010

c# - Get the sum of a datatable column -



c# - Get the sum of a datatable column -

i want build linq query this:

double n = dtcurrentdaterows.asenumerable().where(r => (string)r["date"] == "10/09/2013").sum(r => (double)r["calls"]);

this wrote far:

string c = "calls"; string ymd= "2013-10-09"; string date = string.format("'{0}'", datetime.parse(ymd).tostring("m/dd/yyyy")); double total = dtcurrentdaterows.asenumerable().where(r => (string)r["date"] == date).sum(r => (double)r[c]);

please suggest how sum of datatable column?

edit: trying tdo compute sum of "calls" coulmn date "2013-10-09". don't know how pass in linq query.

update wrote line , works

string ymd = "10/9/2013"; // comes database

string date = datetime.parse(ymd).tostring("m/dd/yyyy");

double sum = dtcurrentdaterows.asenumerable().where(r => (string)r["date"] == date).sum(r => (double)r[columnname]);

but in case when column have blank values gives 0.0.

can suggest me how check if calls blank don't sum otherwise sum values.

try (note: if date field has time values, utilize entityfunctions.truncatetime() remove before comparing ).

system.datetime mydate = new system.datetime(2013, 10, 9); //year, month, date var n = dtcurrentdaterows.asenumerable() .where(r => r.field<system.datetime>("date") == mydate) .sum(r=> r.field<double>("calles"));

c# linq

No comments:

Post a Comment