BattlFrog
BattlFrog

Reputation: 3397

Linq query only works if written in dot notation

I wrote a query in dot notation and it works perfect:

    public IEnumerable<Person> GetPeople()
    {
        return db.Person.OrderBy(m => m.LastName).ThenBy(m => m.FirstName).ToList();
    }

I then had to expand it with some joins, and because I am not that good with dot notation, I did it in query syntax. I was getting a an error "Cant find an implementation of the query pattern" on the Person object. I cut out all the joins and got it down to a query syntax version of the above query that works:

    public IEnumerable<Person> GetPeople()
    {
        var query = from p in Person
                    select p;

        return (query).ToList();
    }

However, the above query is still getting the same error. As far as I can tell, they are the same query. My research shows the usual cause is something not implementing IEnumerable, but these queries are in the same class, 'Repository.cs' so they share the same using statements. So how can one work and the other not?

Upvotes: 1

Views: 235

Answers (2)

Jeff Mercado
Jeff Mercado

Reputation: 134591

Strictly speaking, your query isn't the same as the "dotted" version. Your original is ordered. So aside from the main problem that you're not accessing the Person table correctly, your query should be more like this:

public IEnumerable<Person> GetPeople()
{
    var query = from p in db.Person
                orderby p.LastName, p.FirstName
                select p;

    return query.ToList();
}

Upvotes: 0

mipe34
mipe34

Reputation: 5666

I suppose it should be db.Person which implements the IQueryable.

public IEnumerable<Person> GetPeople()
{
        var query = from p in db.Person
                    select p;

        return (query).ToList();
}

Upvotes: 6

Related Questions