Search code examples
linqcross-referencesupquery

linq sub-query table cross reference


I want to conditionally exclude items from a query of tableA if an ID value for that query is NOT included at least once as a reference value in tableB... Something like this...

Initial query:

var jobs = from j in Jobs select j; // there's more, just keeping it simple...

I have tried these sub-queries...

Optional filtering based on a conditional:

jobs = jobs.Where(j => Bidders.Select(b => b.JobKey == j.JobKey) != null);

OR this:

jobs = jobs.Where(j => Bidders.Select(b => b.JobKey == j.JobKey).Count() > 0);

This does not seem to filter out jobs with no entries in the bidders table... How should I do this???


Solution

  • If there is no navigation property already you were somewhat close with the first approach:

    jobs = jobs.Where(j => Bidders.Any(b => b.JobKey == j.JobKey));