C#C
C#3y ago
İrşat

✅ EF two Select in a query problem when using ToList().

        var monthsTopPosts = _db.Votes
            .Where(v =>
                v.targetPostId != null &&
                v.targetPost!.isPublished == true &&
                v.targetPost!.deletedStatus != null &&
                v.targetPost!.deletedStatus.body == "Default" &&
                v.targetPost!.publishDate > DateTime.Now.AddDays(-30))
            .GroupBy(v => v.targetPostId)
            .Select(g => new
            {
                post = g.First().targetPost,
                karma = g.Sum(v => v.body == true ? 1 : -1)
            })
            .OrderByDescending(k => k.karma)
            .Take(1)
            .ToList()
            .Select(x => x.post)
            .ToList();


I want to reduce two ToList() to one. But it gives me error.

System.InvalidOperationException: The LINQ expression 'ProjectionBindingExpression: 0' 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.
Was this page helpful?