相关疑难解决方法(0)

无法将IEnumerable <T>类型隐式转换为IQueryable <T>

混淆的场景:一个人有零个,一个或多个宠物.

使用Linq to Sql,需要获取IQueryable给定personID的宠物列表.这是ERD中受损严重/被屠宰/混淆的部分:

替代文字

码:

 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
    }
Run Code Online (Sandbox Code Playgroud)

提出异常:

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?) 

尝试失败:

直接投射不起作用: (IQueryable<MyType>)

调用收集方法AsQueryable不起作用: .AsQueryable<MyType>()

问题:

如何正确地将LinqToSql查询的结果转换为IQueryable

linq collections iqueryable linq-to-sql

20
推荐指数
1
解决办法
7万
查看次数

标签 统计

collections ×1

iqueryable ×1

linq ×1

linq-to-sql ×1