0

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???

MalamuteMan
  • 151
  • 1
  • 1
  • 4

1 Answers1

0

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));
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • Thanks BrokenGlass! Duh!!! I don't know why I didn't think of "Any"... Unfortunately, I get the same results... – MalamuteMan Mar 02 '12 at 21:39
  • Above query is correct, can you show an example that illustrates your problem? – BrokenGlass Mar 02 '12 at 21:44
  • Thanks to both BrokenGlass & Phil! The real answer to this question... All of the above work!!! This is **MY BAD!!!** The "Any" solution is clearly the best way to do this... and it IS returning the correct result... The failure occurs at yet another subsequent query which is failing because it is using an inner join where it should be using an outer join. Duh!!! Sorry!!! And thanks for your quick replies... BTW- I will also try adding the navigation property as you suggested. – MalamuteMan Mar 02 '12 at 22:24