6

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 another approach. I ended up rewriting my query without the .Include().

Community
  • 1
  • 1
scw
  • 5,450
  • 8
  • 36
  • 49
  • Have you tried? Doesn't it work? – abatishchev Mar 27 '13 at 21:52
  • Have you Navigation Property mapped? – abatishchev Mar 27 '13 at 21:54
  • p.PeopleClub is valid, but I can't get access to any of the columns in p.PeopleClub. This is the error when I try: 'System.Data.Objects.DataClasses.EntityCollection' does not contain a definition for 'DisplaySequence' and no extension method 'DisplaySequence' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection' could be found (are you missing a using directive or an assembly reference?) – scw Mar 27 '13 at 21:55
  • What is the type of `p.PeopleClub`? Is it a navigation property targeting linking table? – abatishchev Mar 27 '13 at 22:01
  • What version of EF do you use? – abatishchev Mar 27 '13 at 22:03
  • The navigation properties are reflecting the foreign key relationships. PeopleClub is an EntityType. PeopleClubs is listed under the Navigation Properties for People and Club. – scw Mar 27 '13 at 22:12
  • Shouldn't you use `orderby p.Name thenby p.PeopleClub.DisplaySequence`? – Travis J Mar 27 '13 at 22:45
  • 1
    @TravisJ: `orderby a, b` or `OrderBy(a => a).ThenBy(b => b)` – abatishchev Mar 27 '13 at 22:58

1 Answers1

3

Try by 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 join:

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;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • .Include is expecting a string, so it won't let me do that. – scw Mar 27 '13 at 22:02
  • @scw: if you add the namespace below compiler will find an extension method accepting lambda. – abatishchev Mar 27 '13 at 22:06
  • @scw: See a [compilable example](http://yadi.sk/d/1-8UMsIO3aoIA) with both n.p. and join – abatishchev Mar 27 '13 at 22:20
  • What do you mean 'add the namespace below compiler will find an extension method accepting lambda'? – scw Mar 27 '13 at 22:26
  • @scw: I'm talking about [this extension method](http://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx) – abatishchev Mar 27 '13 at 22:27
  • So this would work if I were using EF5? I'll have to check into this tomorrow. – scw Mar 27 '13 at 22:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27122/discussion-between-scw-and-abatishchev) – scw Mar 28 '13 at 18:39