orderby 嵌套 linq

发布于 2024-12-04 11:47:27 字数 428 浏览 0 评论 0原文

var query = from m in db.Members.Include("Companies.Projects.Experiences.ExperienceTags")
                    where m.MemberId == id                        
                    select m;

我也想从这个查询到 orderby the project.enddate。我该怎么做呢。 enddate 也可以为 null,然后我希望它是 orderby 时的今天日期。而且,公司可能并不总是有一个项目,那么它也应该在今天之前订购。

这是 ef 数据图的图像链接

var query = from m in db.Members.Include("Companies.Projects.Experiences.ExperienceTags")
                    where m.MemberId == id                        
                    select m;

I would like to also from this query to orderby the project.enddate. How would I do that. Also enddate can be null which then I would like it to be today date when it orderby. And, a company might not always have a project either which then it should be orderby today date too.

here is a image of the ef data diagram a link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-12-11 11:47:27

由于存在多种关系,您需要像这样执行 SelectMany:

var query = from m in db.Members.Include("Companies.Projects.Experiences.ExperienceTags")
            where m.MemberId == id
            orderby m.Companies.SelectMany(c => c.Projects).OrderByDescending(p => p.EndDate).FirstOrDefault() == null ? 
              DateTime.Today : 
              m.Companies.SelectMany(c => c.Projects).OrderByDescending(p => p.EndDate).FirstOrDefault()          
            select m;

Since there is a many relationship you need to do a SelectMany like so:

var query = from m in db.Members.Include("Companies.Projects.Experiences.ExperienceTags")
            where m.MemberId == id
            orderby m.Companies.SelectMany(c => c.Projects).OrderByDescending(p => p.EndDate).FirstOrDefault() == null ? 
              DateTime.Today : 
              m.Companies.SelectMany(c => c.Projects).OrderByDescending(p => p.EndDate).FirstOrDefault()          
            select m;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文