Cannot implicitly convert type IEnumerable<T> to IQueryable<T>

67,924

Solution 1

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();    

Solution 2

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

Try something like this.

Solution 3

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();
}

Solution 4

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.

Solution 5

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

Share:
67,924

Related videos on Youtube

p.campbell
Author by

p.campbell

Developer in the Microsoft .NET &amp; SQL Server stack. I am focused on delivering great applications in small iterations. I've developed solutions in marketing, healthcare, manufacturing, and transportation verticals. My open source projects on GitHub. Continuously learning.

Updated on July 09, 2022

Comments

  • p.campbell
    p.campbell almost 2 years

    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?