Search code examples
c#linqentity-frameworklinq-to-entitieslinq-query-syntax

Want to sum fee in query month wise


var TotalFee = (from item in _dbEntities.MonthlyFees
                               where item.Year == DateTime.Now.Year &&
                               item.FeeStatus == true
                               select item).GroupBy(x =>x.MonthID);//.AsEnumerable().Sum(x => int.Parse(x));

I want to sum Column "Fee" which is nvarchar type and group it by MonthID


Solution

  • Just look to this example

    var feesDictionary = _dbEntities.MonthlyFees
                .Where(x => x.Year.Equals(DateTime.Now.Year) && x.FeeStatus)
                .GroupBy(x => x.MonthID)
                .ToDictionary(x => x.Key, x => x.Sum(y => Convert.ToDouble(y.Fee)));
    

    The keys are identifiers of months and values - sums of fee for appropriate month.