Cannot implicitly convert type IEnumerable to IQueryable

前端 未结 5 2053
一个人的身影
一个人的身影 2021-02-05 07:46

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 pe

5条回答
  •  再見小時候
    2021-02-05 08:12

    Look at your query:

        var personPets= from p in Person
        where p.ID == somePersonID
        select p.Pets;
    

    What is happening is that you are returning an IEnumerable (of one element) of IEntitySet types (the type: IEnumerable>).

    You should get an IEnumerable and it will be converted to IQueryable by the AsQueryable method:

    public IQueryable GetPersonPets(int personID)
    {
        var person = Person.Single(p=> p.ID == personID);
    
        return person.Pets.AsQueryable();
    }
    

提交回复
热议问题