gpt4 book ai didi

c# - Entity Framework Core 中 TimeSpan 的总和

转载 作者:行者123 更新时间:2023-12-02 16:20:59 25 4
gpt4 key购买 nike

我有一个 View 模型用于在索引页上显示我的类(class)

public class ShowCourseListItemViewModel
{
public int CourseId { get; set; }
public string Title { get; set; }
public string ImageName { get; set; }
public int Price { get; set; }
public TimeSpan TotalTime { get; set; }
}

我有获取所有类(class)的服务:

...
return result.Include(e => e.CourseEpisodes).Select(c => new ShowCourseListItemViewModel()
{
CourseId = c.CourseId,
Title = c.CourseTitle,
ImageName = c.CourseImageName,
Price = c.CoursePrice,
TotalTime = new TimeSpan(c.CourseEpisodes.Sum(e => e.EpisodeTime.Ticks)) <== Error is Here
}).Skip(skip).Take(take).ToList();
...

错误文本:

An unhandled exception occurred while processing the request.InvalidOperationException: The LINQ expression 'DbSet().Where(c0 => EF.Property<Nullable>(EntityShaperExpression:EntityType: Course ValueBufferExpression: ProjectionBindingExpression:EmptyProjectionMember IsNullable: False , "CourseId") != null &&object.Equals( objA:(object)EF.Property<Nullable>(EntityShaperExpression: EntityType:Course ValueBufferExpression: ProjectionBindingExpression:EmptyProjectionMember IsNullable: False , "CourseId"), objB:(object)EF.Property<Nullable>(c0, "CourseId"))) .Sum(c0 =>c0.EpisodeTime.Ticks)' could not be translated. Either rewrite thequery in a form that can be translated, or switch to client evaluationexplicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable','ToList', or 'ToListAsync'. Seehttps://go.microsoft.com/fwlink/?linkid=2101038 for more information.

我认为问题是 TimeSpan 的总和。对这个总结有什么建议吗?

最佳答案

        return result.Include(e => e.CourseEpisodes).AsEnumerable()
.Select(c => new ShowCourseListItemViewModel()
{
CourseId = c.CourseId,
Title = c.CourseTitle,
ImageName = c.CourseImageName,
Price = c.CoursePrice,
TotalTime = new TimeSpan(c.CourseEpisodes.ToList().Sum(e => e.EpisodeTime.Ticks))
}).Skip(skip).Take(take).ToList();

关于c# - Entity Framework Core 中 TimeSpan 的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65524630/

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