In a LINQ query, how to sort by the included column of a table using .Include ()?

I have this table structure: Two tables and a lookup table

I have this LINQ query:

var query = (from p in context.People.Include("PeopleClub").Include("PeopleClub.Club")
             orderby p.Name
             select p).ToList();

Is it possible to do something like this:

var query = (from p in context.People
                              .Include("PeopleClub")
                              .Include("PeopleClub.Club")
             orderby p.Name, p.PeopleClub.DisplaySequence
             select p).ToList();

Update: Using Entity Framework 4.0

Update 2: I give up. See this similar question for a different approach. I ended up rewriting my request without .Include ().

+5
source share
1 answer

Try navigation properties:

using System.Data.Entity;                                  // notice using

var q = from p in context.People
                          .Include(p => p.PeopleClub)      // notice lambda instead of string
                          .Include(p => p.PeopleClub.Club) // you may not need that though
        orderby p.FirstName, p.ClubPerson.DisplaySequence
        select p;

or regular connection:

var q = from p in db.People
        join pc in db.PeopleClub on p.PersonID equals pc.PersonID
        join c in db.Clubs on pc.ClubID equals c.ClubID
        orderby p.FirstName, pc.DisplaySequence
        select p;
+2
source

All Articles