Solved

"Limit" in entity framework?

Posted on 2010-09-20
10
1,994 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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