Solved

Return all row having a maximum value

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 37
Sent email using Shared Mailbox from outlook in C# 5 50
Powershell File Sort 8 41
C# MVC Insert Multiple Row into DB 2 30
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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