
该查询将用于多个不同的目的,每个目的的计算Total属性的方式略有不同.
因为我使用Func来计算总数,所以我得到一个错误,告诉我sql不知道如何处理我的Func的Invoke方法,这是可以理解的.
为了解决这个问题,我不得不通过调用ToList()来将分组列入memor,这对性能不利.
有没有办法可以将此查询作为Iqueryable执行?否则我将不得不用计算差异写20次这个查询
Func<IGrouPing<object,MyType>,double?> calculatetotal= (group) => @group.Sum(x => x.PassengerTotal);Dictionary<object,double?> weekValues = queryable.GroupBy(o => new { Year = sqlFunctions.DatePart("yyyy",o.DateCreated),Week = sqlFunctions.DatePart("ww",Source = o.sourceID,}) .ToList() //NEED TO REMOVE THIS CALL .Select(ac => new WeeklyGraphGroup() { Year = ac.Key.Year,Week = ac.Key.Week,SourceID = ac.Key.source,Total = calculatetotal(ac) }) .ToDictionary(dict => new { Year = dict.Year,Week = dict.Week,Source = dict.sourceID },grp => grp.Total);解决方法 创建一个分部类,如下所示: 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; }} 然后按以下方式进行查询:
var List = db.Stores.GroupBy(o => new WeeklyGraphGroup{Year = sqlFunctions.DatePart("yyyy",}).Select ( u => new WeeklyGraphGroup {Year = u.Key.Year,Week = u.Key.Week,Source = u.Key.source}).ToList(); 总计将自动更新
总结以上是内存溢出为你收集整理的使用c#Func作为IQueryable的一部分而不执行内存调用全部内容,希望文章能够帮你解决使用c#Func作为IQueryable的一部分而不执行内存调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)