Solved

"Limit" in entity framework?

Posted on 2010-09-20
10
1,961 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
  • 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
 
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
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.

 
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 62

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 62

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

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

28 Experts available now in Live!

Get 1:1 Help Now