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'
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments