I have two database tables. Contact (Id, Name, ...) and ContactOperationalPlaces (ContactId, MunicipalityId), where the contact can be connected to several contact operations.
What I'm trying to do is build a query (ASP.NET, C #) using IQueryable, which selects only all the contacts that exist in the ContactOperationalPlaces table with the given MunicipalityId.
The sql query looks like this:
select * from Contacts c right join ContactOperationPlaces cop on c.Id = cop.ContactId where cop.MunicipalityId = 301;
With linq, it will look something like this:
//_ctx is the context var tmp = (from c in _ctx.Contacts join cop in _ctx.ContactOperationPlaces on c.Id equals cop.ContactId where cop.MunicipalityId == 301 select c);
So, I know how to do this, if I had to immediately select all of this, unfortunately, this is not so. I create a query based on user input, so I donβt know all the choices at once.
This is what my code looks like:
IQueryable<Contacts> query = (from c in _ctx.Contacts select c);
The difference is with two statements that with the first, each contact has only one name, but with the last contact can contain several jobs ...
I managed to find one solution, but this solution gives me an unidentified object containing both tables. And I do not know how to do this.
query.Join(_ctx.ContactOperationPlaces, c => c.Id, cop => cop.ContactId, (c, cop) => new {c, cop}).Where(o => o.cop.municipalityId == 301);
The object returned from this expression is System.Linq.Iqueryable <{c: Contact, cop: ContactOperationalPlace}> and cannot be passed to Contacts ...
So this is the problem. The answer is probably pretty simple, but I just can't find it ...