Dave is correct - you are converting working code to LINQ just for the sake of it.
If you still want to use LINQ here, you will need to change your code to:
IQueryable<T> query = (includes ?? Enumerable.Empty<Func<IQueryable<T>, IQueryable<T>>>()).Aggregate(_dbContext.Set<T>().AsQueryable(), (current, include) => include(current));
Breaking that down:
Your original code indicates that
includes
could be
null
. If it is, calling the
Aggregate
extension method on it will throw an
ArgumentNullException
. You need to provide a default value instead. Based on the usage, I have assumed the variable is a list of functions which take an
IQueryable<T>
and return the updated
IQueryable<T>
:
includes ?? Enumerable.Empty<Func<IQueryable<T>, IQueryable<T>>>()
The accumulator function needs to return the same type as the
seed
argument. At the moment,
seed
is
DbSet<T>
, whereas your accumulator function returns
IQueryable<T>
. Add
.AsQueryable()
to your seed argument to correct the type:
.Aggregate(_dbContext.Set<T>().AsQueryable(),
The second argument to the accumulator function will be your
include
object. Based on the code usage, this is a function which transforms an
IQueryable<T>
instance. You are trying to pass this function to the
IQueryable<T>
accumulator, rather than passing the accumulator to the function.
(current, include) => include(current)
As you can see, the resulting LINQ code is less readable, and considerably harder to debug. As Dave said, stick with your existing code instead.