Provide expression for subquery

I have the following LINQ To Entities query (in simplified form):

ctx.BattlesUsers.Where(bu => bu.DateTime == ctx.BattlesUsers.
   Where(BattleUserSpecifications.BattleIdIsEqualTo(bu.BattleId)).
   Max(bu1 => bu1.DateTime));

which throws the exception "Internal error of the .NET Framework 1025 data provider."

The problem here is my specification request. A common solution to this problem is to move the specification request from the request and pass the expression directly to Where. But this will not work here, since I need to pass bu.BattleId to the expression.

Refresh.

Here is the BattleIdIsEqualTo code:

public static Expression<Func<Model.Entities.BattleUser, bool>> UserIdIsEqualTo(long userId)
{
   return bu => bu.UserId == userId;
}
+5
source share
3 answers

If I assume it BattleUserSpecifications.BattleIdIsEqualTo(int battleId)looks similar return bu => bu.BattleId == battleId;, I get the following while working with the new specification:

public static class BattleUserSpecifications
{
    public static Expression<Func<BattleUser, bool>> FilterByDateTime(
        IQueryable<BattleUser> battleUsers)
    {
        return bu => bu.DateTime == battleUsers
            .Where(bu1 => bu1.BattleId == bu.BattleId)
            .Max(bu2 => bu2.DateTime);
    }
    //...
}

:

var query = ctx.BattlesUsers.Where(
    BattleUserSpecifications.FilterByDateTime(ctx.BattlesUsers));

, , , , . , . " " , , , , MS/EF , . , , .

+3

. , ,

    ctx.BattlesUsers.GroupBy(bu => bu.BattleId)
    .Select(bug => bug.OrderByDescending( bu => bu.DateTime).FirstOrDefault())

, , , , Where, .

+1

All Articles