Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2257
  • Last Modified:

"Limit" in entity framework?

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
Nargzul
Asked:
Nargzul
  • 5
  • 3
  • 2
1 Solution
 
DhaestCommented:
Do you mean something like the skip() and take() methods ?

http://www.vikramlakhotia.com/Using_Take_and_Skip_method_in_LINQ_queries.aspx
0
 
NargzulAuthor Commented:
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
 
DhaestCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
NargzulAuthor Commented:
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
 
DhaestCommented:
>> I'm almost sure it will load the full collection.

I'm afraid so ..
0
 
NargzulAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
NargzulAuthor Commented:
@FernandoSoto: doing a .Count() load data no? I just can't afford me this
0
 
Fernando SotoRetiredCommented:
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
 
NargzulAuthor Commented:
Not exactly I was expecting, though I think there is not better solution :/
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now