Is it possible to combine multiple IQueryable into a single one without using Union?

273 Views Asked by At

Good evening. Is it possible to combine multiple IQueryable into a single one without using Union in EF Core 2? The main problem is that it doesn't allow to use methods like .Union(), .Except() etc. It's important that IQueryable should be executed as a single query.

This is what I want to get: toCreateSubquery.Union(toDeleteSubquery)

The queries I want to combine are listed below.

var toCreateSubquery =
    validLinks
        .Where(
            rl => !existingLinks.Any(
                el => el.Contract == rl.Contract && el.Estate == rl.Estate)) // Except() doesn't work'
        .Select(x => new {x.Contract, x.Estate, Type = ActionType.Create});

var toDeleteSubquery =
    existingLinks
        .Where(el => !validLinks.Any(rl => el.Contract == rl.Contract && el.Estate == rl.Estate))
        .Select(x => new {x.Contract, x.Estate, Type = ActionType.Delete});

This is the visualization of the problem I'm solving.

I want to get the union of these sets without intersection and be able to distinguish belonging to one of these sets.

Just in case, I attach the code of getting these sets:

var validLinks = from ac in _context.AreaContracts
                from e in _context.Estate
                where (from al in e.AreaLinks select al.Area.Id).Contains(ac.Area.Id) ||
                      e.Geometry.Intersects(
                          ac.Area.Geometry)
                select new { Contract = ac.Contract.Id, Estate = e.Id };

var existingLinks = from sd in _context.SquareDistributions
    select new { Contract = sd.Contract.Id, Estate = sd.Estate.Id };

Thank you for your attention.

0

There are 0 best solutions below