Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return all row having a maximum value

Posted on 2010-09-09
3
Medium Priority
?
307 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
[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
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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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