dotnet / dotnet/efcore

ToQueryString() and ToDbCommand() variants that accept the entire query as an argument

Open
#33,181 6 comments 6 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

ToQueryString() and ToDbCommand() work on IQueryable, and so cannot be used when e.g. a reducing terminating operator is used (Max(), Min()); it's possible to chop it off, but that changes the SQL. The same limitation applies to ExecuteUpdate/Delete, which behave like other reducing terminating operator.

A solution to this could be ToQueryString()/ToDbCommand() overloads on DbContext, which would accept the entire query as an argument:

```c#
_ = context.ToQueryString(ctx => ctx.Blogs.Max());
```

Note the similarity with the Query() API we've discussed several times in the past, which also accepts a full query as an argument and executes it (i.e. to get all the top-level operators as a quotation rather than execution them).

Note that this would add API noise to the DbContext surface which likely only few people will actually need. Maybe there's some way to combine ToQueryString() with the aforementioned Query() API, to help out with this.

Raised by @clement911 in https://github.com/dotnet/efcore/issues/33143#issue-2150085896 and #33167.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.