简体   繁体   English

MAX在实体框架的Where子句中

[英]MAX in Where Clause in Entity Framework

I have SQL Query like this 我有像这样的SQL查询

SELECT WI.[WorkItemID]  ,[WorkItemNumber]  ,[ToDate]     
FROM [WorkItem] AS WI INNER JOIN  [WorkItemTrack] AS WT on WI.WorkItemID=WT.WorkItemID
WHERE MAX([ToDate]) BETWEEN @StartDate AND @EndDate

and corresponing Entity Framework Query is 和对应的实体框架查询是

 workItems = from wi in workItems.Where(p => p.IsActive)
             join wt in entityCollection.WorkItemTrack on wi.WorkItemID equals wt.WorkItem.WorkItemID
             where wt.ToDate >= fromdate && wt.ToDate <= todate
             select wi;

Here I am not able to use MAX for wt.ToDate in Enitity Framework Query 在这里,我无法在Enitity Framework Query中使用MAX作为wt.ToDate

Please help me 请帮我

Have you tried SqlServer.Max() method. 你试过SqlServer.Max()方法吗? these are aggregate methods you can use 这些是您可以使用的聚合方法

Example

SELECT VALUE SqlServer.MAX(p.ListPrice)
FROM AdventureWorksEntities.Product as p 

The max in the where clause shouldn't work. where子句中的max应该不起作用。

"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference." “聚合可能不会出现在WHERE子句中,除非它位于HAVING子句或选择列表中包含的子查询中,并且要聚合的列是外部引用。”

But if you want the top most result based on a certain column, try adding an order by said column, then take the top result of that set. 但是,如果您想要基于特定列的最高结果,请尝试按所述列添加订单,然后获取该集合的最高结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM