在linq-to-entities的一对多连接中只获得一条(最后一条)记录

时间:2011-10-27 03:08:25

标签: c# linq join linq-to-entities one-to-many

我在linq-to-entities中有以下内容

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

合作伙伴名称

城市

区域

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

3 个答案:

答案 0 :(得分:8)

如果您在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()
    ;

答案 1 :(得分:1)

听起来你正试图提出以下问题:

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甚至可能无法编译,但希望它会向您发送正确的方向。

答案 2 :(得分:0)

对不起,我不会说你的语言。但是,例如,在MySql中你可以添加sort by update_dt DESC LIMIT 1你可以这样做或者某些类似的东西吗?