ToList () invalid evaluation when using Include

I have the following snippet:

IQueryable<BaseEntity> someQueryble = ApplicationDbContext.BaseEntities;
var randomQueryable = someQueryable.OfType<RandomEntity>()
                                   .Include(prop => prop.SomeRandomNavProp)
                                   .OrderBy(prop => prop.Id);

BaseEntitiesis just DbSetfrom ApplicationDbContextfrom EF 6. RandomEntityinherits from BaseEntity(table hierarchy for each type)

Then something strange happens:

randomQueryable.Take(10).Count();
10
randomQueryable.Take(10).ToList().Count;
20

When Includeremoved, everything works fine. Why .includedoes it affect ToListand how can I resolve it?

Edit: generated SQL:

http://pastebin.com/SS3WD7P6

Caution is from a production database, so readability can be harmful. The first request is Count, and the second is aboutToList()

+4
source share
1 answer

@Evk @IvanStoev Guid.NewGuid(): fooobar.com/questions/53876/...

( ):

public static IOrderedQueryable<BaseEntity> Randomize(
                         this IQueryable<BaseEntity> queryable)
{
    var seed = Random.NextDouble();

    return queryable.OrderBy(o => SqlFunctions.Checksum(o.Id * seed))
                     .ThenBy(o => o.Id);
}
+1

Source: https://habr.com/ru/post/1659646/


All Articles