首页 > 解决方案 > IQueryable 上的 EF Core “InvalidOperationException:InvalidOperationException:Include 已用于非实体可查询”

问题描述

标签: c#entity-frameworklinq.net-coreentity-framework-core

解决方案


If you change the query so that it no longer returns instances of the entity type that the query began with, then the include operators are ignored.

So your code is giving error. You can do like below where you can use Includes in your first query and use OrderByDescending ToListAsync to next.

var postsQuery = _ctx.PostTag
                .Include(st => st.Post)
                    .ThenInclude(s => s.AppUser)
                .Include(st => st.Post)
                    .ThenInclude(s => s.PostTags)
                        .ThenInclude(st => st.Tag)
                .Where(st => st.TagId == {provided TagId})
                .Select(st => st.Post);
                
var postsWithExtraData = postsQuery                            
                        .OrderByDescending(s => s.TotalVoteCount)
                        .ToListAsync();

推荐阅读