简体   繁体   English

LINQ:将集合中的所有时间跨度相加/求和

[英]LINQ: add/sum all timespans in a collection together

I'm trying to simply add up all the TimeSpans in my ViewModel, but I keep getting an error from this (and I can assure you that 'x.totalDuration' is in fact a TimeSpan and that 'myCollection' is indeed an IEnumerable: 我试图在ViewModel中简单地添加所有TimeSpans,但是我不断从中得到一个错误(并且我可以向您保证'x.totalDuration'实际上是TimeSpan,并且'myCollection'实际上是IEnumerable:

View: 视图:

  var myCollection = Model.Select(x => x.totalDuration);
  var ts = new TimeSpan(myCollection.Sum(r => r.Ticks)); <-- boom here--<

Controller: 控制器:

 [HttpGet]
    public ActionResult List()
    {
        var model = _db.Tracks
          .Where(r => r.UserID == WebSecurity.CurrentUserId)
          .Select(x => new iOSHistoryListViewModel
          {
              averagePace = Math.Round(26.8224 / x.locations.Average(y => y.speed), 2),
              createdDate = x.createdDate,
              FirstName = x.UserProfile.FirstName,
              totalDistance = Math.Round(x.totalDistance / 1609.344, 2),
              totalDuration = TimeSpan.FromSeconds(x.totalDuration),
              trackId = x.TrackID
          })
          .OrderByDescending(x => x.createdDate)
          .ToList(); <-- tried with/without this

        return View(model);
    }

Error: 错误:

LINQ to Entities does not recognize the method 'System.TimeSpan FromSeconds(Double)' method, and this method cannot be translated into a store expression.

Description: An unhandled exception occurred during the execution of the     current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.TimeSpan FromSeconds(Double)' method, and this method cannot be translated into a store expression.

Source Error: 


Line 13:         @{
Line 14:             var myCollection = Model.Select(x => x.totalDuration);
Line 15:             var ts = new TimeSpan(myCollection.Sum(r => r.Ticks));

This is so simple, yet I am missing something... 这很简单,但我缺少一些东西...

The problem is you are trying to call TimeSpan.FromSeconds in the database. 问题是您正在尝试在数据库中调用TimeSpan.FromSeconds。 This is a .NET method and can't be translated. 这是一个.NET方法,无法翻译。 This is untested, but to fix try this: 这未经测试,但是要解决此问题,请尝试以下操作:

        var model = _db.Tracks
      .Where(r => r.UserID == WebSecurity.CurrentUserId)
      .Select(x => new
      {
          averagePace = Math.Round(26.8224 / x.locations.Average(y => y.speed), 2),
          createdDate = x.createdDate,
          FirstName = x.UserProfile.FirstName,
          totalDistance = x.totalDistance,
          totalDuration = x.totalDuration,
          trackId = x.TrackID
      }).ToList()
      .Select(x => new iOSHistoryListViewModel{
          averagePace = x.averagePace,
          createdDate = x.createdDate,
          FirstName = x.FirstName,
          totalDistance = Math.Round(x.totalDistance / 1609.344, 2),
          totalDuration = TimeSpan.FromSeconds(x.totalDuration),
          trackId = x.trackId
      })
      .OrderByDescending(x => x.createdDate)
      .ToList();

The key here is that the first Select throws in the data returned from the data source into a list. 这里的关键是第一个Select将从数据源返回的数据放入一个列表中。 The list is then in .NET memory where you can do any .NET operations. 然后,该列表位于.NET内存中,您可以在其中执行任何.NET操作。 Then send that result set to a new list as well. 然后将结果集也发送到新列表。 That should get rid of your exception about .FromSeconds 那应该摆脱您关于.FromSeconds的例外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM