You can calculate the sum of any column in datatable using linq like this:


In your case it will be like this:

table.AsEnumerable().Where(x=>x.Field<int>("J") < 340 && x.Field<int>("J") > 365).Sum(x=>x.Field<int>("J"));

Where condition you can modify as per your need. In above where condition it will not calculate J values which are between 340 to 365


Is this what you need?

//find the max year
var lastRecord = table.AsEnumerable()
    .OrderByDescending(f => f.Field<DateTime>("Dates")).First();
//if you want to filter by predefined year - change the value of lastYear
var lastYear = lastRecord.Field<DateTime>("Dates").Year;

var aggregates = table.AsEnumerable()
    //grouping by J and Year to exclude uninterested records
    .GroupBy(f => new
            J = f.Field<int>("J"), 
            Year = f.Field<DateTime>("Dates").Year 
    //excluding records with unsatisfactory conditions
    .Where(f => (f.J < 340 || f.J  > 365) && f.Year != lastYear)
    //combining remaining records into one sequence
    .SelectMany(f => f)
    .GroupBy(f => f.J)
    .Select(f => new 
        J = f.Key,
        SumValues = f.Sum(x => x.Field<double>("Values"));

The resulting sequence will contain sums with J flag.

Related Query

More Query from same tag