Obfuscated Scenario: A person has zero, one or many pets.
Using Linq to Sql, the need is to get an IQueryable
list of pets for the given personID.
Here's the poorly mangled/butchered/obfuscated portion of the ERD:
Code:
public IQueryable<Pet> GetPersonPets(int personID)
{
var personPets= from p in Person
where p.ID == somePersonID
select p.Pets;
return personPets; //fail
// return (IQueryable<Pet>)personPets //also fail
// return personPets.AsQueryable<Pet>() //also fail
}
Exception Raised:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable (System.Data.Linq.EntitySet(Pet))' to 'System.Linq.IQueryable(Pet)'. An explicit conversion exists (are you missing a cast?)
Failed Attempts:
Direct casting didn't work: (IQueryable<MyType>)
Calling collection method AsQueryable
didn't work: .AsQueryable<MyType>()
Question:
How can you cast the results of the LinqToSql query properly to IQueryable
?