2015-02-23 55 views
0

这里有一个表,我有:enter image description here如何总结和LINQ组数据

反正我想在LINQ做的是一组由LOGDATE总结feedGiven列(我们可以截断时间..它不需要)与productionCycleID。

然后取最后7天给定的饲料总和并将其平均。

目前这里是我现在:

public IQueryable<FeedingLog> getLastSevenDaysAverage(int intendedProductionCycleID) 
    { 
     var ListProductionCycleQuery = this.ObjectContext.FeedingLogs.Where(b => b.ProductionCycleID== intendedProductionCycleID); 

     var SumResult = from s in ListProductionCycleQuery 
         group s by new {s.ProductionCycleID,s.feedGiven, s.LogDate} into g 
         select new {g.Key.ProductionCycleID, g.Key.LogDate g.Sum(y => y.feedGiven)}; 

     return ListProductionCycleQuery; 
    } 

我认真的LINQ难住了,通常我会做的SQL,但实体框架是有点越野车,如果我有一个观点去(没有主键的问题)

我也有代码这个问题:

enter image description here

基本上它说无效的匿名类型我mber申报人。匿名类型的memmer必须用成员赋值声明。 预先感谢阅读,并在其中feedGiven组中的解决方案

+5

通过feedGiven如果你想总结一下不群。你会在SQL中这样做吗? =) – 2015-02-23 14:09:35

+0

生产周期ID始终为220,为什么需要对它进行分组,除非它是可变的? – THBBFT 2015-02-23 14:14:37

+0

@thbbft问题是该列表正被过滤到只有220. – 2015-02-23 14:31:36

回答

1

,但是这是一个领域,你要作为分组

var SumResult = from s in ListProductionCycleQuery 
       group s by new {s.ProductionCycleID,s.LogDate} into g 
       select new {g.Key.ProductionCycleID, g.Key.LogDate g.Sum(y => y.feedGiven)}; 

另外的一部分,您要从函数返回原始查询,您可能需要定义一个类来表示分组/汇总信息并返回该信息。

0

略去的GroupBy子句对feedGiven:

var SumResult = from s in ListProductionCycleQuery 
    group s by new {s.ProductionCycleID,s.LogDate} into g 
    select new {g.Key.ProductionCycleID, g.Key.LogDate g.Sum(y => y.feedGiven)};