C # EF 6.1 - Strict conditional query behavior with delegate

I noticed a very strange behavior of Entity Framework 6.1. My goal is to conditionally count some rows from a table filled with 2 million records. First, I used a simple method to get the desired value:

private void SeedsQuery(object o)
{
    StatProperty property = o as StatProperty;

    using (SteelPoppyContext context = new SteelPoppyContext())
    {
            property.Value = context.QueuedGames.Count(game => game.IsSeed);
    }
}

This code runs for about 1 second and provides the desired result without using any memory. Now I would like to use more conditions for the same request using the switch statement. The easiest way is to use a delegate, which is provided each time a request is made. To simplify our case, I prepared a simple example.

private void SeedsQuery(object o)
{
    StatProperty property = o as StatProperty;
    Func<QueuedGame, bool> predicate = new Func<QueuedGame, bool>(game => game.IsSeed);

    using (SteelPoppyContext context = new SteelPoppyContext())
    {
            property.Value = context.QueuedGames.Count(predicate);
    }
}

, . , , 1 , - 1 . , Entity Framework , . ?

+4
1

,

. , .

Count Expression<Func<T, bool>>, Func<T, bool>. , , , :

private void SeedsQuery(object o)
{
    StatProperty property = o as StatProperty;
    Expression<Func<QueuedGame, bool>> predicate = x => x.IsSeed;

    using (SteelPoppyContext context = new SteelPoppyContext())
    {
            property.Value = context.QueuedGames.Count(predicate);
    }
}

, predicate, , Expression<Func<QueuedGame, bool>>, Func<QueuedGame, bool>.

EF SQL , , , .

+7

All Articles