linq按多列分组并计数

pqwbnv8z  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(357)

我的.net核心应用程序中有以下linq代码。我使用efcore和pomero作为mysql的驱动程序。

var journeyId = 5917;
var journey = Journeys.FirstOrDefault(j => j.JourneyId == journeyId);

var journeyEvents = from ad in AccelerometerData
                    join ae in AccelerometerEvents on ad.AccelerometerDataId equals ae
                        .AccelerometerData.AccelerometerDataId
                    where ad.Device.DeviceId == journey.Device.DeviceId && ae.TimeStamp >= journey.StartDateTime &&
                          ae.TimeStamp <= journey.EndDateTime
                    group ae by new
                    {
                        ae.EventType,
                        ae.Level
                    } into g
                    select new
                    {
                        EventType = new JourneyEventType { JourneyEventTypeId = g.Key.EventType },
                        Level = g.Key.Level,
                        Count = g.ToList().Count()
                    };

journey.JourneyEvents = journeyEvents.ToList();

当代码尝试运行最后一行时,出现以下异常。
无法分析表达式“g.tolist()”:当前不支持方法“system.linq.enumerable.tolist”的此重载。
我错过了什么?

jtw3ybtb

jtw3ybtb1#

使用g.count()代替g.tolist().count()。

var journeyEvents = from ad in AccelerometerData
                    join ae in AccelerometerEvents on ad.AccelerometerDataId equals ae
                        .AccelerometerData.AccelerometerDataId
                    where ad.Device.DeviceId == journey.Device.DeviceId && ae.TimeStamp >= journey.StartDateTime &&
                          ae.TimeStamp <= journey.EndDateTime
                    group ae by new
                    {
                        ae.EventType,
                        ae.Level
                    } into g
                    select new
                    {
                        EventType = new JourneyEventType { JourneyEventTypeId = g.Key.EventType },
                        Level = g.Key.Level,
                        Count = g.Count()
                    };

journey.JourneyEvents = journeyEvents.ToList();
2guxujil

2guxujil2#

我想你应该在这里选择:

g.SelectToMany(gr => gr).ToList().Count()

相关问题