Entity Framework Core / SQLite: GroupJoin after Jo

2019-02-20 02:05发布

问题:

Using Entity Framework Core 2.1 and an SQLite database, I get different behaviour from a LINQ GroupJoin if I use it after another Join. It is not clear if this is a bug or if there is something I am overlooking.

I have created a minimal VS2017 project to reproduce this, which can be found here:
https://gitlab.com/haddoncd/EntityFrameworkGroupJoinIssue

In this example, I get one object for each row in the Blog table, each containing multiple PostTitles:

db.Blogs
    .GroupJoin(
        db.Posts,
        s => s.BlogId,
        p => p.BlogId,
        (s, ps) => new
        {
            s.BlogId,
            s.BlogTitle,
            PostTitles = ps.Select(p => p.PostTitle),
        }
    )
    .OrderBy(x => x.BlogId))

But in this example, the results are "flattened" - I get one object for each Blog-Post pair like you would from a conventional SQL outer join:

db.Subscriptions
    .Join(
        db.Blogs,
        s => s.BlogId,
        b => b.BlogId,
        (s, b) => new
        {
            s.SubscriptionId,
            b.BlogId,
            b.BlogTitle,
        }
    )
    .GroupJoin(
        db.Posts,
        x => x.BlogId,
        p => p.BlogId,
        (x, ps) => new
        {
            x.SubscriptionId,
            x.BlogTitle,
            PostTitles = ps.Select(p => p.PostTitle),
        }
    )
    .OrderBy(x => x.SubscriptionId))