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
. If it is, calling the
extension method on it will throw an
. 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
and return the updated
includes ?? Enumerable.Empty<Func<IQueryable<T>, IQueryable<T>>>()
The accumulator function needs to return the same type as the
argument. At the moment,
, whereas your accumulator function returns
to your seed argument to correct the type:
The second argument to the accumulator function will be your
object. Based on the code usage, this is a function which transforms an
instance. You are trying to pass this function to the
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.