-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DbContext.Query for arbitrary rootless queries #29484
Comments
@smitpatel to fill in detail. |
public T Query<TContext, T>(Expression<Func<TContext, T>> expression)
{
return DbContextDependencies.QueryProvider.Execute<T>(expression);
}
public T QueryAsync<TContext, T>(Expression<Func<TContext, T>> expression, CancellationToken cancellationToken = default)
{
return DbContextDependencies.QueryProvider.ExecuteAsync<T>(expression, cancellationToken);
} Usage: context.Query(ctx => new
{
userCount = ctx.Users.Count(),
animalCount = ctx.Animals.Count()
});
context.Query(ctx => EF.Functions.StandardDeviationSample(ctx.Products.Select(u => u.UnitPrice)));
context.Query(ctx => string.Join(", ", ctx.Products.Select(p => p.Name)));
// Can also combine above 2
context.Query(ctx => new {
userCount = ctx.Users.Count(),
Names = string.Join(", ", ctx.Users.Select(e => e.Name))
}); Or something like this on derived DbContext [DbFunction]
public int PostReadCount(int postId)
{
return Query(ctx => ctx.PostReadCount(postId));
} Translations SELECT (SELECT COUNT(*) FROM [Users] AS [u0]) AS [userCount],
(SELECT COUNT(*) FROM [Animals] AS [a]) AS [animalCount];
SELECT STDEVP([p].[UnitPrice])
FROM [Products] AS [p];
SELECT dbo.PostReadCount(@postId) For databases which doesn't support SELECT without FROM would use Translation of first is very straightforward, we just translate individual subquery and put on projection. |
Covers issue #27728 by allowing users to use API to run multiple different scalar returning queries in 1 round-trip. |
Possibly covers also #30159, which is about allowing arbitrary expressions as arguments of top-level TVF-mapped functions. |
No description provided.
The text was updated successfully, but these errors were encountered: