获取按日期排序的不同列

发布于 2024-10-26 00:45:08 字数 530 浏览 2 评论 0原文

我有以下两个表:

Job Title | PostDate | CompanyId
Assitant  | 12/15/10 | 10
Manager   | 12/1/10  | 11
Developer | 12/31/10 | 10
Assitant  | 12/1/10  | 13
PM        | 11/29/10 | 12

CompanyId | Name
10        | Google
11        | Yahoo
12        | Microsoft
13        | Oracle

现在我想要 3 家不同的公司,其职位按发布日期排序。结果表如下:

Job Title | PostDate | CompanyName
Developer | 12/31/10 | Google
Manager   | 12/1/10  | Yahoo
Assitant  | 12/1/10  | Oracle

如何使用 linq 查询实现这一点?任何帮助将不胜感激...

I have these following two tables:

Job Title | PostDate | CompanyId
Assitant  | 12/15/10 | 10
Manager   | 12/1/10  | 11
Developer | 12/31/10 | 10
Assitant  | 12/1/10  | 13
PM        | 11/29/10 | 12

CompanyId | Name
10        | Google
11        | Yahoo
12        | Microsoft
13        | Oracle

Now i would like to get 3 different companies with the jobs sorted by post date. The result table would be following:

Job Title | PostDate | CompanyName
Developer | 12/31/10 | Google
Manager   | 12/1/10  | Yahoo
Assitant  | 12/1/10  | Oracle

How can I achieve that using a linq query? Any help will be appreciated...

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

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

发布评论

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

评论(1

苍景流年 2024-11-02 00:45:13

我认为这会是这样的:

var query = from company in db.Companies
            join job in db.Jobs on company.CompanyId equals job.CompanyId
            group job by company into jobsByCompany
            let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
                                       .First()
            orderby lastJob.PostDate descending
            select new
            {
                JobTitle = lastJob.JobTitle,
                PostDate = lastJob.PostDate,
                CompanyName = jobsByCompany.Key.Name
            };

先进行连接然后再进行分组有点奇怪 - 我们可以先进行 GroupJoin,然后丢弃空选项:

var query = from company in db.Companies
            join job in db.Jobs on company.CompanyId equals job.CompanyId
                 into jobsByCompany // Make this a group join
            let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
                                       .FirstOrDefault()
            where lastJob != null
            orderby lastJob.PostDate descending
            select new
            {
                JobTitle = lastJob.JobTitle,
                PostDate = lastJob.PostDate,
                CompanyName = company.Name
            };

编辑:请注意,这不仅仅需要前三名的结果。使用 query = query.Take(3); 仅获取前三个结果。

I think that would be something like:

var query = from company in db.Companies
            join job in db.Jobs on company.CompanyId equals job.CompanyId
            group job by company into jobsByCompany
            let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
                                       .First()
            orderby lastJob.PostDate descending
            select new
            {
                JobTitle = lastJob.JobTitle,
                PostDate = lastJob.PostDate,
                CompanyName = jobsByCompany.Key.Name
            };

It's a little odd to do a join and then a group - we could do a GroupJoin instead, but then discard empty options:

var query = from company in db.Companies
            join job in db.Jobs on company.CompanyId equals job.CompanyId
                 into jobsByCompany // Make this a group join
            let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
                                       .FirstOrDefault()
            where lastJob != null
            orderby lastJob.PostDate descending
            select new
            {
                JobTitle = lastJob.JobTitle,
                PostDate = lastJob.PostDate,
                CompanyName = company.Name
            };

EDIT: Note that doesn't just take the top three results. Use query = query.Take(3); to just get the first three results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文