'How to build Linq Query setp by step to retrieve data from DBSet
I am building a dynamic query with some predicates, some may be a .Contains(), others a .StartsWith(), ...
But when I try to retrieve the entities I get a weird EF Core error:
System.InvalidOperationException: 'The LINQ expression 'DbSet() .Where(t => Invoke(__predicate_0, t.EndsOn) )' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
IQueryable<Api.Data.Models.Travel> query = _travelContext.Travels.AsQueryable();
foreach(var predicate in travelQuery.Predicates.StartDate)
{
query = query.Where(travel => predicate(travel.StartedOn));
}
foreach (var predicate in travelQuery.Predicates.EndDate)
{
query = query.Where(travel => predicate(travel.EndsOn));
}
var x = await query.ToListAsync();
Console.WriteLine(x);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
