Skip to content

Translate custom aggregate functions without GroupBy #29200

@roji

Description

@roji

Max can be used over a collection navigation in a projection:

_ = await ctx.Authors
    .Select(a => new { Author = a, Books = a.Books.Max(b => b.Id) })
    .ToArrayAsync();
SELECT [a].[Id], [a].[Name], (
    SELECT MAX([b].[Id])
    FROM [Books] AS [b]
    WHERE [a].[Id] = [b].[AuthorId]) AS [Books]
FROM [Authors] AS [a]

However, the same can't be done with string.Join:

_ = await ctx.Authors
    .Select(a => new { Author = a, Books = string.Join(", ", a.Books.Select(b => b.Name)) })
    .ToArrayAsync();

(this causes client evaluation)

The query above can be rewritten using a GroupBy, which does work:

_ = await ctx.Books
    .GroupBy(b => b.Author)
    .Select(g => new { Author = g.Key, Books = string.Join(", ", g.Select(b => b.Name)) })
    .ToArrayAsync();

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions