3

我有近 100 万条数据,我绝对不想将它们全部查询到内存然后对它们进行分组,所以我尝试了这个,但是 linq2Entity 无法识别 string.join()、IEnumerable.toList() 或相同的事物

                var capaHrs = (
                from newsPerHr in _dbRawDataContext.CapacityPerHours
                where newsPerHr.StreamDT >= this.BeginDateTime
                                && newsPerHr.StreamDT < this.EndDateTime
                                && newsPerHr.ServerName == this.ServerName
                group newsPerHr by newsPerHr.StreamDT into grp
                let MsgSizeTotal = (from i in grp select i.MsgSize).Sum(x => (int)x)
                let MNumber = (from i in grp select i.MsgNumber).Sum(x => (int)x)
                let Feeds = (from i in grp select i.FeedName).ToArray()
                select new 
                {
                    ServerName = ServerName,
                    FeedName = Feeds,
                    StreamDT = grp.Key,
                    MsgSize = MsgSizeTotal,
                    MsgNumber = MNumber
                }).ToList();

            _capacityData = capaHrs.Select(x => new CapacityPerHour {
                ServerName = x.ServerName,
                FeedName = string.Join(", ",x.FeedName),
                StreamDT = x.StreamDT,
                MsgSize = x.MsgSize,
                MsgNumber = x.MsgNumber
            }).ToList();

如何使它工作?

4

1 回答 1

2

它只需要几个更改:

  1. 删除ToArray了 Feed 声明中的
  2. 按匿名类型分组,因此您可以包含多个属性,ServerName并且StreamDT

试试这个:

var capaHrs = (
                from newsPerHr in _dbRawDataContext.CapacityPerHours
                where newsPerHr.StreamDT >= this.BeginDateTime
                                && newsPerHr.StreamDT < this.EndDateTime
                                && newsPerHr.ServerName == this.ServerName
                group newsPerHr by new { newsPerHr.ServerName, newsPerHr.StreamDT } into grp
                let MsgSizeTotal = grp.Sum(x => (int)x.MsgSize)
                let MNumber = grp.Sum(x => (int)x.MsgNumber)
                let Feeds = (from i in grp select i.FeedName)
                select new 
                {
                    ServerName = grp.Key.ServerName,
                    FeedName = Feeds,
                    StreamDT = grp.Key.StreamDT,
                    MsgSize = MsgSizeTotal,
                    MsgNumber = MNumber
                }).ToList();

_capacityData = capaHrs.Select(x => new CapacityPerHour {
                ServerName = x.ServerName,
                FeedName = string.Join(", ",x.FeedName),
                StreamDT = x.StreamDT,
                MsgSize = x.MsgSize,
                MsgNumber = x.MsgNumber
            }).ToList();
于 2012-10-18T14:40:08.233 回答