views:

50

answers:

2

I want to do paging outside of my Repository, but the Repository returns IList. Since I don't want to pull data unnecessarily, and don't want to create specific Paging methods in my Repositories, Is there any way to filter records by page in the Where extension method?

I want to do something like:

var myRecords = ProductRepo.Get( p => p.Name.StartsWith("Pepsi") &&
                                      p.Row_Number() > 9 &&
                                      p.Row_Number() < 21);

The Get repository code (doesn't handle row number of course):

public IList<Product> Get( Expression<Func<Product, bool>> filter = null)
{
    if( filter == null)
        return _dc.Products.ToList<Product>();
    else
        return _dc.Products.Where(filter).ToList<Product>();
}
+1  A: 

Like that?

var myRecords = ProductRepo.Get( p => p.Name.StartsWith("Pepsi") ).Skip(9).Take(12);
Ahmet Kakıcı
The problem is that .Get retrieves all the records in to a List. .Skip and .Take would filter the already retrieved records. If it's done in the Where clause, the skipped records would never leave the database.
Dr. Zim
Ahmet Kakıcı
That's really the root of the problem. Given that you return an IList and that you don't have Take and Skip in the Repository, how could you achieve the same effect?
Dr. Zim
Get user defined or not?
Ahmet Kakıcı
Dr. Zim
+2  A: 

Return IQueryable from your Repository (don't call .ToList()). You will be able to use Skip and Take then.

ROWNUM is not supported in EF.

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/055911ff-6db6-48cb-8e50-bd6415b7eefe/

Jakub Konecki
:) Lets say that the Repository returning IList is part of the requirement. I know SQL server has a ROW_NUMBER() function. I was just curious how we could add that to the where clause?
Dr. Zim