在子查询中具有最大日期的linq

时间:2019-04-05 15:32:00

标签: c# .net linq

我需要选择一条记录,该记录连接到具有状态的另一个表并插入DateTime。我想要对应于具有最大Date_Created

的AgreementStatuss的记录
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>'

1 个答案:

答案 0 :(得分:0)

感谢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