question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union fails if one query has returned no results

See original GitHub issue

Union fails if one query in the union returns no result. An InvalidOperationException is thrown with the message "When performing a set operation, both operands must have the same include operations. Investigation revealed that if one of the queries in the union is empty (returned no results) the union operation fails. In code example below any or the queries (1, 2, or 3) may return no results.

        public async Task<DelimitedList> AvailableClubsAsync(string clubMnemonic = null)
        {
            using var context = new PGSSqlServerContext();

            var query1 = context.Clubs.ForGolferAsync().Where(cl => cl.ClubMnemonic == clubMnemonic)
                                                                                   .Select(cl => new { cl.ClubMnemonic, Sequence = 0 });

            var query2 = context.Clubs.ForGolferAsync().Where(cl => !cl.Courses.Any())
                                                                                   .Select(cl => new { cl.ClubMnemonic, Sequence = 1 });

            var numberOfTees = await context.Tees.ForGolferAsync().CountAsync().ConfigureAwait(false);

            var query3 = context.Courses.ForGolferAsync().Where(co => co.ClubMnemonic != clubMnemonic)
                                                                                       .GroupBy(co => new { co.ClubMnemonic, co.CourseName })
                                                                                       .Where(g => (g.Count() < numberOfTees && g.Key.CourseName == null) || (g.Key.CourseName != null))
                                                                                       .Select(g => new { g.Key.ClubMnemonic, Sequence = 1 });

            var query = query1.Union(query2).Union(query3)
                                          .OrderBy(q => q.Sequence).ThenBy(q => q.ClubMnemonic)
                                          .Select(q => q.ClubMnemonic);

            return new DelimitedList(list: await query.ToListAsync());
        }

Further technical details

EF Core version: 3.1.1 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.1) Operating system: Windows 10 IDE: (e.g. Visual Studio 2019 164…3)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:30 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
FormerMarinecommented, Apr 24, 2020

I tested a very small subset of a large data access layer. In addition to this problem I also encountered a problem with DefaultIfEmpty not accepting a default value. The performance of various queries was significantly worse than EF6.

I will definitely try again when we get a little closer to the 5.0 release date.

On Fri, Apr 24, 2020 at 7:04 AM Shay Rojansky notifications@github.com wrote:

Sorry to hear that @FormerMarine https://github.com/FormerMarine. It would be good to know exactly which problems you ran into, and hopefully you’ll try again at some point.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/efcore/issues/19705#issuecomment-618995679, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALZAM764NMA4ERD7H4TVRFTROGE5XANCNFSM4KLQPOLA .

1reaction
rojicommented, Apr 23, 2020

Note that this has already been fixed in 5.0.0-preview1 - you can give that a try as well. It would be great to get confirmation that the bug is gone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - One of the query in Union returns no results in postgres
1 Answer. First, use union all unless you want to incur the overhead of removing duplicates. Second, the union / union all should...
Read more >
UNION IF query problem - Forums
Now my problem is, if either part of the query has no results, I want a result returned with zero values. Using IF...
Read more >
Use a union query to combine multiple queries into ...
If the union query fails to run, you can adjust each query individually until it succeeds and then rebuild your union query with...
Read more >
How to make an empty result in a union query NULL
I'm wondering if anyone here may know. In a query with multiple unions, is there a way to manually set their result set...
Read more >
“UNION query, returning null if both scripts contain values ...
In the instance when both sides of the union returns a value I need the script to either return a NULL or Error,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found