Solved

"Limit" in entity framework?

Posted on 2010-09-20
10
1,928 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

12 Experts available now in Live!

Get 1:1 Help Now