Link to home
Start Free TrialLog in
Avatar of BobRosas
BobRosas

asked on

Largest value of a field (type int) in C#

I have a field (RcdId) which is of Int type.  I would like to extract the value of the largest one in the table.  In this example I want the result of 236.  I posted code using Max and Asc but they do not work in C#.  Your help is appreciated.
Emp No         RcdId
101      235
102      236
103      222
104      24
105      91

My environment is Visual Studio, the language is C# and I'm attached to a SQL Server db.

var LastRcdId = (from c in dbTimeClockPlus.EmployeeLists where Max(c.RecordId)ASC select c.RecordId).FirstOrDefault();

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try this:

var LastRcdId = (from c in dbTimeClockPlus.EmployeeList select c.RecordId).Max(c.RecordId);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of p_davis
p_davis

pretty much a repeat of kaufmed's solution.
//using System.Linq;
int LastRcdId = dbTimClockPlus.EmployeList.Max(rec => rec.RecordId);
Avatar of BobRosas

ASKER

Thank you both for your quick response.  Kaufmed was 1st and it worked like a charm!  So I awarded him points but I think all you EE guys are great!  Thanks again!
NP. Glad to help.   = )
ya, didn't expect points just offering another way to do the same thing.
glad you got your answer.