Cannot implicitly convert type IEnumerable to IQueryable

前端 未结 5 2055
一个人的身影
一个人的身影 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:18

    This works for me (with different tables of course, but same relationship):

    IQueryable personPets = (
       from p in db.Person
       where p.ID == somePersonID
       select p
    ).Single().Pets.AsQueryable();
    

    Although I'd probably write it in some variation of this way:

    var personPets = 
        db.Person.Single(t => t.Id == somePersonId).Pets.AsQueryable();    
    

提交回复
热议问题