0

I guess this should be straight forward, but what is the correct way of filtering a related table in Linq to SQL. It works when I bring in the secondary table explicitely with a new join, but I'm sure filtering on the related table should also work.

For example:

var q = from p in db.Personnel
        where p.PersonnelGifts.Where(p => p.GiftValue >= 2477)
        select {...}

I get the error that

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'

Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77

1 Answers1

2
var q = from p in db.Personnel
    where p.PersonnelGifts.Where(p => p.GiftValue >= 2477).Any()
    select {...}

or as @Jon Skeet pointed out - .Any() also accepts predicate, so you can write it like

var q = from p in db.Personnel
    where p.PersonnelGifts.Any(p => p.GiftValue >= 2477)
    select {...}

Why your code didn't work? .Where() returns IEnumerable (or IQueryable for that matter) so you can chain it with another LINQ methods which accepts IEnumerable as parameter. Your where clause expects bool value and exactly this type is returned by .Any() method.

Paweł Hemperek
  • 1,130
  • 10
  • 21