gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:39:49 24 4
gpt4 key购买 nike

我有这个表结构: 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() 的情况下重写了我的查询。

最佳答案

通过导航属性尝试:

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;

关于c# - 在 LINQ 查询中,如何使用 .Include() 按包含的表列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15670189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com