我有近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();
如何让它发挥作用?
答案 0 :(得分:2)
只需要进行一些更改:
ToArray
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();