Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LINQ aggregate function max()

Status
Not open for further replies.

murrayja

Programmer
May 6, 2000
90
US
I have inherited a LINQ project.
I am trying to do the equivalent of "select max(empid) from employees" using LINQ.

My statement is var mx = (from EmpID in db.EmployeeMains select EmpID).Max(); and it barfs at the max() part.
EmployeeMains is a table with column EmpID as an int.

I could work around but I would like to know how to use aggregates in linq.
 
I think it is getting confused over the use of EmpID. The EmpID immediately following the "from" is really just an alias for the type. the "EmpID" immediately following the "select" is what you are returning... in this case, the entire EmployeeMain object. Max() is not likely a function you can perform on the EmployeeMain object unless you put together some crazy comparator

try this

Code:
var mx = (from e in db.EmployeeMains
          select e.EmpID).Max();

In this example, "e" is the alias for the EmployeeMain type and I am selecting only the EmpID property (e.EmpID), which the Max() aggregate is performed on.

Good luck!

-Mark

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top