简体   繁体   中英

MAX in Where Clause in Entity Framework

I have SQL Query like this

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

Please help me

Have you tried SqlServer.Max() method. 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.

"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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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