首页 > 解决方案 > 需要从另一个表中获取最大日期值才能按产品列表排序

问题描述

我有两张桌子。

第一个表是产品表如下:

Product
Id    Name
1     Apple
2     Desktop
3     Laptop

ProductStatus
Id   ProductId LatestUpdatedDate
1      2       01-01-2021
2      2       08-07-2021

我期待以下结果:

2 Desktop
1 Apple
3 laptop

如何将此查询转换为 linq?

我尝试的样本

var query= _ProductRepository.Table;

 var productStatusHistory = (from psh in _productStatusHistoryRepository.Table
                                            group psh by psh.ProductId into g
                                            select g.OrderByDescending(t => t.CreatedOnUtc).FirstOrDefault());

                query = (from q in query
                         join ps in productStatusHistory on q.Id equals ps.ProductId into l_ps
                         from ps in l_ps.DefaultIfEmpty()
                         let index = ps == null ? default(DateTime) : ps.CreatedOnUtc
                         orderby index
                         select q);

此查询工作正常。我想将此作为单个查询

标签: linq

解决方案


推荐阅读