Solved

Get MAX(val) using Entities

Posted on 2013-01-13
6
345 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now