linq在子查询中有最大日期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq在子查询中有最大日期相关的知识,希望对你有一定的参考价值。

我需要选择一个连接到具有status和插入DateTime的另一个表的记录。我希望与AgreementStatuses对应的记录具有最大Date_Created

from c in Agreements 
    join a in AgreementStatuses on c.Agreements_ID equals a.Agreements_ID
    join s in Statuses on a.Statuses_ID equals s.Statuses_ID
where DateTime.Now > c.BeginDate
    && DateTime.Now < c.TermDate
    && c.ControlPlan_ID == 31
    && s.StatusText == "Promoted"
    && c.AlphaPrefix == "PEH"
    && c.States_ID == 33
    && a.Date_Created == (from n in AgreementStatuses  //ERROR HERE
        join y in Statuses on n.Statuses_ID equals y.Statuses_ID
        where y.StatusText == "Promoted"
        && n.Agreements_ID == 584
        group n by n.Agreements_ID into g
        select new {Date_Created = g.Max(t=>t.Date_Created)}).FirstOrDefault()
select c

错误:

Operator '==' cannot be applied to operands of type 'DateTime' and '<anonymous type: DateTime Date_Created>'
答案

谢谢jdweng。这样做了。

from c in Agreements 
    join a in AgreementStatuses on c.Agreements_ID equals a.Agreements_ID
    join s in Statuses on a.Statuses_ID equals s.Statuses_ID
where DateTime.Now > c.BeginDate
    && DateTime.Now < c.TermDate
    && c.ControlPlan_ID == 31
    && s.StatusText == "Promoted"
    && c.AlphaPrefix == "PEH"
    //&& c.States_ID == 33
    && a.Date_Created == (from n in AgreementStatuses 
        join y in Statuses on n.Statuses_ID equals y.Statuses_ID
        where y.StatusText == "Promoted"
        && n.Agreements_ID == a.Agreements_ID
        group n by n.Agreements_ID into g
        select g.Max(t=>t.Date_Created)).FirstOrDefault()
select c

以上是关于linq在子查询中有最大日期的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 代码中的 DB 中的 Linq 查询中包含空单元格

可以使用 DateTime 列表过滤 List<object> 的 Linq 查询

如何在子查询中填写日期?

在子查询中显示特定日期减去 14 天的所有行

在 Linq-to-Entities 查询中格式化日期会导致异常

使用LINQ在数组中查找最小和最大日期?