Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - LINQ - Use Expressions in Select

I am using within my code some EF LINQ expressions to keep complex queries over my model in one place:

public static IQueryable<User> ToCheck(this IQueryable<User> queryable, int age, bool valueToCheck = true)
{
    return queryable.Where(ToBeReviewed(age, valueToCheck));
}

public static Expression<Func<User, bool>> ToCheck(int age, bool valueToCheck = true)
{
    return au => au.Status == UserStatus.Inactive
        || au.Status == UserStatus.Active &&
        au.Age.HasValue && au.Age.Value > age;
}

I am then able to use them in queries:

var globalQuery = db.Users.ToCheck(value);

And also in selects:

var func = EntityExtensions.ToCheck(value);

var q = db.Department.Select(d => new
{
    OrdersTotal = d.Orders.Sum(o => o.Price),
    ToCheck = d.Users.AsQueryable().Count(func),
})

What I am trying to achieve is to actually use the same expression/function within a select, to evaluate it for each row.

var usersQuery = query.Select(au => new {
    Id = au.Id,
    Email = au.Email,
    Status = au.Status.ToString(),
    ToBeChecked = ???, // USE FUNCTION HERE
    CreationTime = au.CreationTime,
    LastLoginTime = au.LastLoginTime,
});

I am pretty that threre would be a way using plain EF capabilities or LINQKit, but can't find it.

like image 412
Benjamin Soulier Avatar asked Dec 09 '25 11:12

Benjamin Soulier


1 Answers

Answering my own question :)

As pointed by @ivan-stoev, the use of Linqkit was the solution:

var globalQueryfilter = db.Users.AsExpandable.Where(au => au.Department == "hq");

var func = EntityExtensions.ToCheck(value);
var usersQuery = globalQueryfilter.Select(au => new
{
    Id = au.Id,
    Email = au.Email,
    Status = au.Status.ToString(),
    ToBeChecked = func.Invoke(au),
    CreationTime = au.CreationTime,
    LastLoginTime = au.LastLoginTime,
});
return appUsersQuery;

It's required to use the AsExpandable extension method from Linqkit along with Invoke with the function in the select method.

like image 191
Benjamin Soulier Avatar answered Dec 12 '25 10:12

Benjamin Soulier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!