0

I try to run this code here, in ASP.NET Entity Framework 7. The target is to have the most efficient solution here which should be a JOIN if the database.

public async Task<List<Building>> GetAllAsync(string commaSeparatedBuildingIDs)
    {
        var buildingRefIDs = commaSeparatedBuildingIDs.Split(",").ToList();

            return await semiSoftDbContext.Buildings
            .Join(buildingRefIDs, building => building.ReferenceId, refID => refID, (building, id) => building)
            .ToListAsync();

    }

I get the following error:

System.InvalidOperationException: The LINQ expression 'DbSet() .Join( inner: __p_0, outerKeySelector: building => building.ReferenceId, innerKeySelector: refID => refID, resultSelector: (building, id) => building)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.`

I already have tried several variations of join and read through the internet, but it says that ASP.Net should work with Join().

0

1 Answer 1

1

You should not join here (assuming you are using Entity Framework - AFAIK it does not handle quite a lot of operations with local collections, like joins), use Where with Contains:

return await semiSoftDbContext.Buildings
   .Where(b => buildingRefIDs.Contains(b.ReferenceId))
   .ToListAsync();
3
  • but the solution of "Where - Contains" is O(n²) if I am not mistaken (?) where join would only be O(n) because it is based on a hash function. How could we achieve O(n) instead?
    – Max Aigner
    Jan 30 at 15:21
  • 1
    @MaxAigner in this case Where will be translated into SQL (most likely into WHERE .. IN (...)) and it is up to database how to handle that. I highly doubt that there would be such huge performance difference. Check the generated SQL and compare the execution plan with the SQL you can write using JOIN.
    – Guru Stron
    Jan 30 at 15:24
  • Ok so I will test it as soon as I have the time.
    – Max Aigner
    Feb 1 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.