Solved

Return all row having a maximum value

Posted on 2010-09-09
3
290 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

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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