LINQ to Entities无法识别方法'System.String [] ToArray [String]

时间:2012-10-18 14:31:11

标签: c# entity-framework linq-to-entities group-by

我有近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();

如何让它发挥作用?

1 个答案:

答案 0 :(得分:2)

只需要进行一些更改:

  1. 删除了Feed中的ToArray
  2. 以匿名类型分组,因此您可以添加多个属性,ServerNameStreamDT
  3. 尝试一下:

    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();