在带有link -to-entities的一对多连接中仅获取一条(最后一条)记录

本文关键字:一条 获取 最后 记录 连接 link -to-entities 一对多 | 更新日期: 2023-09-27 18:12:26

我有以下链接到实体

clientprojects = (from p in this.SAPMappingEntities.SAP_Master_Projects 
join c in this.SAPMappingEntities.SAP_Master_ProjectPartners on c.project_no equals p.project_no
where c.partner_name.Contains(clientstring)
orderby p.start descending 
select new ClientProjects { client = c.partner_name, location = c.city +", "+c.region, project_no = c.project_no, start_dt = p.start, end_dt = p.finish }).Take(50).ToList();

我想更改这个查询,以便对于每个SAP_Master_Project只获取具有最新update_dt的SAP_Master_ProjectPartners记录。我该怎么做呢?

编辑

有一个项目表,其中包含项目编号和项目详细信息,包括项目开始和结束日期。有一个项目合作伙伴表,其中包含项目合作伙伴编号、名称、项目编号、更新日期和其他详细信息。

SAP_MASTER_PROJECT

project_no

开始

完成

SAP_MASTER_PROJECTPARTNERS

partner_no

project_no

partner_name

城市

地区

update_dt

当用户在文本框中输入"ABC"时,我想要返回的信息是项目编号、项目开始日期、项目结束日期加上项目合作伙伴名称、城市和州,这些信息来自最近50个项目(基于开始日期)的最后一个项目合作伙伴记录,其中项目合作伙伴名称包含或类似于"ABC"。

我确信有不止一种方法可以做到这一点,但他的SQL给了我需要的结果:

SELECT TOP 50 p.project_no, p.start, p.finish, c.partner_name, c.city, c.region
FROM 
(select pp.project_no, pp.partner_name, pp.city, pp.region
from SAP_Master_ProjectPartners pp
where pp.partner_name LIKE @clientstring AND pp.update_dt = (select max(pp1.update_dt)
                       from SAP_Master_ProjectPartners pp1
                       where pp1.project_no = pp.project_no)) c
join SAP_Master_Projects p
on (p.project_no = c.project_no)
ORDER BY p.start DESC

编辑# 2 该sql实际上返回几个具有相同update_dt的项,因此我将sql修改为如下所示。

仍在努力转换为linq.
SELECT TOP 50 p.project_no, p.start, p.finish, c.partner_name, c.city, c.region, c.update_dt, c.row_id
FROM SAP_Master_Projects p
join
(select pp.project_no, pp.partner_name, pp.city, pp.region, pp.update_dt, pp.row_id
from SAP_Master_ProjectPartners pp
where pp.partner_name LIKE @clientstring AND pp.row_id = (select TOP 1 row_id
                       from SAP_Master_ProjectPartners pp1
                       where pp1.project_no = pp.project_no order by update_dt DESC)) c
on (p.project_no = c.project_no) where p.active_flag = 1
ORDER BY p.start DESC

在带有link -to-entities的一对多连接中仅获取一条(最后一条)记录

如果在SAP_Master_ProjectsSAP_Master_ProjectPartners之间定义一个实体关系,那么这个查询可能会更简单,因此连接可以是隐式的而不是显式的。

编辑既然你不能这样做,这样的事情可能会起作用(使用let并在where子句中进行逻辑连接):

var clientProjects =
    (
        from p in entities.SAP_Master_Projects
        let c = entities.SAP_Master_ProjectPartners
            .Where(cl => cl.partner_name.Contains(clientstring)
                && cl.project_no == p.project_no
                )
            .OrderBy(cl => cl.update_dt) // Todo: Might need to be descending?
            .FirstOrDefault()
        where c != null
        orderby p.start descending
        select new ClientProjects
        {
            client = c.partner_name,
            location = c.city + ", " + c.region,
            project_no = c.project_no,
            start_dt = p.start,
            end_dt = p.finish
        }
        )
    .Take(50)
    .ToList()
    ;

听起来您想要提出以下查询:

SELECT *
  FROM MasterProjects p
       INNER JOIN (SELECT project_no,
                          partner_name
                     FROM ProjectPartners o
                    WHERE o.update_dt = (SELECT MAX(update_dt)
                                           FROM ProjectPartners i
                                          WHERE i.project_no = o.project_no)) c
               ON p.project_no = c.project_no
              AND p.partner_name = c.partner_name

我不完全确定如何将其翻译成LINQ,但这是我最好的尝试:

var clientprojects =
    from p in MasterProjects
    join c in ProjectPartners on p.project_no == c.project_no
   where c.partner_name == (from o in ProjectPartners
                           where o.project_no == c.project_no
                             and o.update_dt == (from i in ProjectParters
                                                where o.project_no = i.project_no
                                               select i.update_dt).Max()
                          select o.partner_name).First();

上面的LINQ可能无法编译,但希望它能给你指明正确的方向。

对不起,我不会说你的语言。但是,例如,在MySql中,你可能会添加sort by update_dt DESC LIMIT 1你能这样做吗?