在LINQ查询中,如何使用包含的表列使用.Include()进行排序?

时间:2013-03-27 21:49:53

标签: c# linq entity-framework linq-to-entities sql-order-by

我有这个表结构: Two tables and a lookup table

我有这个LINQ查询:

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

是否可以做这样的事情:

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

更新:使用Entity Framework 4.0

更新2:我放弃了。有关其他方法,请参阅this similar question。我最后在没有.Include()的情况下重写了我的查询。

1 个答案:

答案 0 :(得分:2)

尝试导航属性:

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;

或定期加入:

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;