How do I 'build' a QUERY with EF Core?
Essentially I had an endpoint that can take multiple query string params, most are optional, so I want to check first if the query parameter exists - then if it does "add" it to the query. This keeps to be sending me errors and doesn't feel efficient; have I missed something?
12 Replies
Like this
Ahh, so build the queryable; then it is called when you
.ToListAsync()
Yep
Interesting!
.ToListAsync()
, .FirstOrDefaultAsync()
, CountAsync()
etc. are where the query gets resolvedSo I guess my current impl, is just executing the query then I'm transforming the shape of the data based on my
Your current code doesn't really do anything
Since the query doesn't get modified in place
You just call the
.Where()
on the query... and discard itGood:
Bad:
Ahhhhh! So just update the query with it
All those methods — besides the ones that resolve the query — return an
IQueryable
, they're not void
YeahThanks!