无法隐式转换 IEnumerable 类型 到 IQueryable

发布于 2024-07-26 07:00:58 字数 1122 浏览 3 评论 0 原文

混淆场景:一个人有零只、一只或多只宠物。

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

alt text

代码:

 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
    }

引发异常:

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)

调用集合方法 AsQueryable 没有不起作用: .AsQueryable()

问题

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

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:

alt text

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

亽野灬性zι浪 2024-08-02 07:00:58

这对我有用(当然使用不同的表,但关系相同):

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

虽然我可能会用这种方式的一些变体来编写它:

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

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

IQueryable<Pet> 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();    
滿滿的愛 2024-08-02 07:00:58
List<Pet> personPets = 
   (from p in Persons
   where p.ID == somePersonID
   select p.Pets).ToList();

尝试这样的事情。

List<Pet> personPets = 
   (from p in Persons
   where p.ID == somePersonID
   select p.Pets).ToList();

Try something like this.

星軌x 2024-08-02 07:00:58

查看您的查询:

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

发生的情况是您正在返回 IEntitySet 类型的 IEnumerable(一个元素)(类型:IEnumerable> ;)。

您应该得到一个 IEnumerable,它会被 IQueryable 转换为 IQueryable >AsQueryable 方法:

public IQueryable<Pet> GetPersonPets(int personID)
{
    var person = Person.Single(p=> p.ID == personID);

    return person.Pets.AsQueryable();
}

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<Pet> types (the type: IEnumerable<IEntitySet<Pet>>).

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

public IQueryable<Pet> GetPersonPets(int personID)
{
    var person = Person.Single(p=> p.ID == personID);

    return person.Pets.AsQueryable();
}
银河中√捞星星 2024-08-02 07:00:58

我有以下内容并且效果很好。 我用上面提到的两个表设置了一个简单的数据库,并使用 VS 生成数据类。

var db = new DataClasses1DataContext();
var personPets = from p in db.Persons
                 where p.PersonId == 1
                 select p.Pet;

在我看来,您的 Person 实际上是 Class 而不是数据库对象(默认情况下由代码生成器命名)。 首先测试上述内容是否适合您,有时调试器可能只是给您一些奇怪的原因,而这些原因实际上并没有指出真正的问题。

I have the following and it works perfectly. Which I setup a simple database with your above mentioned two tables, and generate the dataclass with VS.

var db = new DataClasses1DataContext();
var personPets = from p in db.Persons
                 where p.PersonId == 1
                 select p.Pet;

It looks like to me, your Person is actually the Class instead of the database object (which is by default named by the code generator). Test if the above works for you first, sometimes the debugger may simply give you some freaky reason which is not actually pointing to the real problem.

方圜几里 2024-08-02 07:00:58

,什么对我有用

    var db = new DataClasses1DataContext();
var personPets = from p in db.Persons
                 where p.PersonId == 1
                 select p.Pet;
IQuerable<Pet> pets = (IQuerable<Pet>)personPets;

奇怪的是

what worked for me,

    var db = new DataClasses1DataContext();
var personPets = from p in db.Persons
                 where p.PersonId == 1
                 select p.Pet;
IQuerable<Pet> pets = (IQuerable<Pet>)personPets;

strangely enough

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文