Entity Framework: Efficiently grouping by month

后端 未结 3 2079
春和景丽
春和景丽 2020-12-06 01:59

I\'ve done a bit of research on this, and the best I\'ve found so far is to use an Asenumerable on the whole dataset, so that the filtering occurs in linq to objects rather

3条回答
  •  执念已碎
    2020-12-06 02:36

    When it comes to group by month i prefer to do this task in this way:

    var sqlMinDate = (DateTime) SqlDateTime.MinValue;
    
    var trendData = ExpenseItemsViewableDirect
        .GroupBy(x => SqlFunctions.DateAdd("month", SqlFunctions.DateDiff("month", sqlMinDate, x.Er_Approved_Date), sqlMinDate))
        .Select(x => new
        {
            Period = g.Key // DateTime type
        })
    

    As it keeps datetime type in the grouping result.

提交回复
热议问题