سلام استاد بنده با استفاده از کد زیر هرچه تلاش کردم دیتا رو نشون بدم به مشکل زیر بر خورد کردم:
public List<ShowCourseListItemViewModel> GetPopularCourse()
{
return _context.Courses
.Include(c => c.OrderDetails)
.Where(c => c.OrderDetails.Any())
.OrderByDescending(d => d.OrderDetails.Count)
.Take(8)
.Select(c => new ShowCourseListItemViewModel()
{
CourseId = c.CourseId,
ImageName = c.CourseImageName,
Price = c.CoursePrice,
Title = c.CourseTitle,
TotalTime = new TimeSpan(c.CourseEpisodes.Sum(e => e.EpisodeTime.Ticks))
})
.ToList();
}
خطا:
An unhandled exception occurred while processing the request.
InvalidOperationException: The LINQ expression 'DbSet<CourseEpisode>()
.Where(c0 => EF.Property<Nullable<int>>(EntityShaperExpression:
EntityType: Course
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
, "CourseId") != null && object.Equals(
objA: (object)EF.Property<Nullable<int>>(EntityShaperExpression:
EntityType: Course
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
, "CourseId"),
objB: (object)EF.Property<Nullable<int>>(c0, "CourseId")))
.Sum(c0 => c0.EpisodeTime.Ticks)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, ref <>c__DisplayClass15_0 )
InvalidOperationException: The LINQ expression 'DbSet<CourseEpisode>() .Where(c0 => EF.Property<Nullable<int>>(EntityShaperExpression: EntityType: Course ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False , "CourseId") != null && object.Equals( objA: (object)EF.Property<Nullable<int>>(EntityShaperExpression: EntityType: Course ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False , "CourseId"), objB: (object)EF.Property<Nullable<int>>(c0, "CourseId"))) .Sum(c0 => c0.EpisodeTime.Ticks)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
حتی با جابه جا کردن select مشکل رو حل کردم اما هرچی دوره میخریدم محبوب ها درست نمیشدند و همیشه مقدار null بود.
سرچ کردم هرکاری کردم نشد.
لطفا میگید چطوری درستش کنم؟
سلام
خط زیر را بردارید
در ادامه دوره توضیح دادم
TotalTime = new TimeSpan(c.CourseEpisodes.Sum(e => e.EpisodeTime.Ticks))