【问题标题】:Entity Linq - Retrieve record and only the first child record in a one to many relationshipEntity Linq - 检索记录并且仅检索一对多关系中的第一个子记录
【发布时间】:2010-10-08 05:31:09
【问题描述】:

我有一个名为“Requests”的实体,它有一个名为“StatusHistories”的导航

我需要检索最后一个 StatusHistory 为“Open”的所有请求

StatusHistory 有字段 StartDate(其中最高的一个将是最后一个 StatusHistory) 状态(此假定状态包含字符串“Open”或“Closed”) RecordID(这是SQL Server中的一个Identity字段,所以也可以用来查找最后一个,但我宁愿不要)

谢谢。

【问题讨论】:

    标签: linq entity-framework linq-to-entities


    【解决方案1】:
    var result = from r in Requests
                 where <condition> 
                 select r.field1, r.field2, (from s in StatusHistory
                                             where <join codition>
                                             order by s.StartDate descending
                                             select s.field).FirstOrDefault()
    

    【讨论】:

    • 我也建议给最后一个字段起个名字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多