Solved

Return all row having a maximum value

Posted on 2010-09-09
3
284 Views
Last Modified: 2013-11-11
Hi!

I'm using entity framework to connect the database.

I've a table(Let's call it "File") that haves several fields:

ID, Version, XYZ
Primarky key  is based on ID AND Version.
so I can have several line with the same ID but different version(and inversly).

The question is:

How can I, with a LAMBDA expression, ask my Entity Framework, to return me all  last version of a "file".

Example:
Datas:
ID;Version;Other
1;1;YX
1;2;YZ
2;1;AH
2;2;BH
2;5;CA
1;3;AAA

Result:
1;3;AAA
2;5;CA

Thank you!

!! The goal is that the database doesn't need to return all rows, and is called only one time, so forget solution like GetAllRows and read the whole collection and save only the latest, or get a list of all possible ID and get the last version foreach in another request. Thanks!
0
Comment
Question by:Nargzul
3 Comments
 
LVL 9

Expert Comment

by:rfportilla
ID: 33634613
0
 
LVL 1

Author Comment

by:Nargzul
ID: 33634638
I'm sorry but I don't understand. doing a Top/limit mean that all my row I want to get are on the top.

but I can't order them correctly, because, if I order by version the previous example and make a top(2) on this example datas:
ID;Version;Other
1;1;YX
1;2;YZ
2;1;AH
2;2;BH
2;4;CA
2;5;CA
1;3;AAA

I will get two results for the "ID" 2.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33634771
You could try something like:
SampleDBEntities db = new SampleDBEntities();



var files = from file in db.File

            group file by file.FileID into grouped

            from g in grouped

            where g.Version == grouped.Max(m => m.Version)

            select g;

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

11 Experts available now in Live!

Get 1:1 Help Now