Somashekhar
Somashekhar

Reputation: 513

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

Upvotes: 3

Views: 1700

Answers (2)

Rohan B&#252;chner
Rohan B&#252;chner

Reputation: 5393

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.

Upvotes: 1

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

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 

Upvotes: 1

Related Questions