gpt4 book ai didi

c# - LINQ Group By 多列并进行计数

转载 作者:行者123 更新时间:2023-11-29 06:36:53 31 4
gpt4 key购买 nike

我的 .NET Core 应用程序中有以下 LINQ 代码。我使用 EF Core 和 Pomelo 作为 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();

当代码尝试运行最后一行时,出现以下异常。

Could not parse expression 'g.ToList()': This overload of the method 'System.Linq.Enumerable.ToList' is currently not supported.

我错过了什么?

最佳答案

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

关于c# - LINQ Group By 多列并进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53434183/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com