linq按多列分组并计数

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

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

  1. var journeyId = 5917;
  2. var journey = Journeys.FirstOrDefault(j => j.JourneyId == journeyId);
  3. var journeyEvents = from ad in AccelerometerData
  4. join ae in AccelerometerEvents on ad.AccelerometerDataId equals ae
  5. .AccelerometerData.AccelerometerDataId
  6. where ad.Device.DeviceId == journey.Device.DeviceId && ae.TimeStamp >= journey.StartDateTime &&
  7. ae.TimeStamp <= journey.EndDateTime
  8. group ae by new
  9. {
  10. ae.EventType,
  11. ae.Level
  12. } into g
  13. select new
  14. {
  15. EventType = new JourneyEventType { JourneyEventTypeId = g.Key.EventType },
  16. Level = g.Key.Level,
  17. Count = g.ToList().Count()
  18. };
  19. journey.JourneyEvents = journeyEvents.ToList();

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

jtw3ybtb

jtw3ybtb1#

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

  1. var journeyEvents = from ad in AccelerometerData
  2. join ae in AccelerometerEvents on ad.AccelerometerDataId equals ae
  3. .AccelerometerData.AccelerometerDataId
  4. where ad.Device.DeviceId == journey.Device.DeviceId && ae.TimeStamp >= journey.StartDateTime &&
  5. ae.TimeStamp <= journey.EndDateTime
  6. group ae by new
  7. {
  8. ae.EventType,
  9. ae.Level
  10. } into g
  11. select new
  12. {
  13. EventType = new JourneyEventType { JourneyEventTypeId = g.Key.EventType },
  14. Level = g.Key.Level,
  15. Count = g.Count()
  16. };
  17. journey.JourneyEvents = journeyEvents.ToList();
展开查看全部
2guxujil

2guxujil2#

我想你应该在这里选择:

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

相关问题