Solved

"Limit" in entity framework?

Posted on 2010-09-20
10
2,107 Views
Last Modified: 2013-11-11
Hi!

How can I with entity framework, do some "limit" request, e.g. in my case, I want to return the 3 last object.

So, how to do this on an IQueryable object?

Thank you!
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
  • 5
  • 3
  • 2
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33715474
Do you mean something like the skip() and take() methods ?

http://www.vikramlakhotia.com/Using_Take_and_Skip_method_in_LINQ_queries.aspx
0
 
LVL 1

Author Comment

by:Nargzul
ID: 33715524
Not exactly, because, if we have something like this:
Skip(x).Take(y)

I only want to specify the Y.

I know I can OrderByDescending, Take and after this OrderBy, but this will make two sort instead of one, it's costly
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33715579
I'm afraid that there will be no other way, then to reverser the result (like you already did) to get the records.

I'm not sure, but maybe you can use the elementAt(...)

Something like this:

Dim newaccount = From n In db.accounts.ElementAt(db.accounts.Count() - 3).Take(3)
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:Nargzul
ID: 33715607
I don't think, because ElementAt return my object type and no more an IQueryable object.

It should be possible to do something like

From n In db.accounts.Skip(db.accounts.Count() - 3).Take(3), but I'm almost sure it will load the full collection.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33715675
>> I'm almost sure it will load the full collection.

I'm afraid so ..
0
 
LVL 1

Author Comment

by:Nargzul
ID: 33715759
So have you an idea to avoid that? The table will contains a lot of data, and even if the table will be cleaned regularly, it will not be enough. because this will be called on every page of my remote application
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33716793
Hi Nargzu;

First get a count of the records and then use a Skip( count - 3 ). For example using the Adventureworks DB as the source the following will work.

DataClasses1DataContext db = new DataClasses1DataContext();

int recCount = db.Employee.Count();

var emp = (from E in db.Employee
           select E).Skip(recCount - 3);

var result = emp.ToList();

Fernando
0
 
LVL 1

Author Comment

by:Nargzul
ID: 33838986
@FernandoSoto: doing a .Count() load data no? I just can't afford me this
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33840442
Then the only way to do what you want is to reverse order the query and take the top 3, something like the following.

     var query = (from E in db.Employee
                         orderby E.EmployeeID descending
                         select E).Take(3);
0
 
LVL 1

Author Closing Comment

by:Nargzul
ID: 33840516
Not exactly I was expecting, though I think there is not better solution :/
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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