Solved

Get MAX(val) using Entities

Posted on 2013-01-13
6
353 Views
Last Modified: 2013-01-14
How do I get the MAX value from a field in table?

Something like:
Using myContext as new myEntities
    Dim x as integer = (from y in myContext.myTable Select MAX(ID)
End Using
0
Comment
Question by:lippert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:deepu chandran
ID: 38773374
Hi,

Try Max function like below, i dont have a system to check below statement

 Dim x as integer = myContext.myTable.Max(p => p.ID)

-- Deepu
0
 
LVL 1

Author Comment

by:lippert
ID: 38773388
'p' needs to be declared. What is it?
0
 
LVL 1

Author Comment

by:lippert
ID: 38773390
What I'm really after here is the equivalent of the SQL statements:
     DECLARE x as int
     Select x = MAX(ID) From myTable
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 38773423
Hi,

Try it like this:

var max = from mx in myContext.myTable
                  select mx;

int maxValue = max.Max(p=>p.ID);


Regards,
Mishu
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 38773971
Try following

 Dim x as integer = myContext.myTable.Max(Function(x) x.ID)
0
 
LVL 1

Author Closing Comment

by:lippert
ID: 38774333
Thanks for putting me on the right track. Your solution didn't work exactly as posted, but it was a minor change:

Dim x as integer = myContext.myTable.Max(Function(z) z.ID)

Couldn't reuse the x in the function because the compiler complained about the variable being hidden.
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question