Use a c# Func as part of an IQueryable without executing into memory call

笑着哭i 提交于 2019-12-05 19:05:09

Create a partial class as follows:

public partial class WeeklyGraphGroup
{
    public int ? Year { get; set; }

    public int  ? Week { get; set; }

    public int Source { get; set; }

}

public partial class WeeklyGraphGroup
{
    private int ? _Total;

    public int ? Total
    {


        get
        {


            this._Total = CalculateTotal(this.Year, this.Week, this.Source);

            return this._Total;
        }

    }

    public int ? CalculateTotal(int ? Year, int ? Week, int Source)
    {

        // do your calculation and return the value of total
        // use whatever formula you want here. I guess you are calculating 
        // total based on any of the parameters(year, week or source);

        return value;

    }



}

Then do your query as below:

var list = db.Stores.GroupBy(o => new WeeklyGraphGroup
{
Year = SqlFunctions.DatePart("yyyy", o.DateCreated),
Week = SqlFunctions.DatePart("ww", o.DateCreated),
Source = o.SourceId,
})
.Select ( u => new WeeklyGraphGroup 
{
Year = u.Key.Year,
Week = u.Key.Week,
Source = u.Key.Source
}
).ToList();

Total will be updated automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!