Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:07:54+00:00 2026-05-27T01:07:54+00:00

I am using Entity Framework 4.1 and the repository pattern . I am trying

  • 0

I am using Entity Framework 4.1 and the repository pattern.

I am trying to create methods that will be used throughout most scenarios. I am trying to create a method that brings back records and sorts it according to the order criteria supplied. It can be sorted by 1, 2 or 3 columns. I want this to be specified. I found the following code in the Orchard framework.

In the IRepository interface they have the following (I left out the other methods):

public interface IRepository<T>
{
     IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate);
     IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order);
}

The implementation for IEnumerable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order); is:

public class Repository<T> : IRepository<T>
{
     public virtual IQueryable<T> Fetch(Expression<Func<T, bool>> predicate)
     {
          return Table.Where(predicate);
     }

     public virtual IQueryable<T> Fetch(Expression<Func<T, bool>> predicate, Action<Orderable<T>> order)
     {
          var orderable = new Orderable<T>(Fetch(predicate));
          order(orderable);
          return orderable.Queryable;
     }
}

The Orderable class:

public class Orderable<T>
{
     private IQueryable<T> _queryable;

     public Orderable(IQueryable<T> enumerable)
     {
          _queryable = enumerable;
     }

     public IQueryable<T> Queryable
     {
          get { return _queryable; }
     }

     public Orderable<T> Asc<TKey>(Expression<Func<T, TKey>> keySelector)
     {
          _queryable = _queryable
               .OrderBy(keySelector);
          return this;
     }

     public Orderable<T> Asc<TKey1, TKey2>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2)
     {
          _queryable = _queryable
               .OrderBy(keySelector1)
               .OrderBy(keySelector2);
          return this;
     }

     public Orderable<T> Asc<TKey1, TKey2, TKey3>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2,
          Expression<Func<T, TKey3>> keySelector3)
     {
          _queryable = _queryable
               .OrderBy(keySelector1)
               .OrderBy(keySelector2)
               .OrderBy(keySelector3);
          return this;
     }

     public Orderable<T> Desc<TKey>(Expression<Func<T, TKey>> keySelector)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector);
          return this;
     }

     public Orderable<T> Desc<TKey1, TKey2>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector1)
               .OrderByDescending(keySelector2);
          return this;
     }

     public Orderable<T> Desc<TKey1, TKey2, TKey3>(Expression<Func<T, TKey1>> keySelector1,
          Expression<Func<T, TKey2>> keySelector2,
          Expression<Func<T, TKey3>> keySelector3)
     {
          _queryable = _queryable
               .OrderByDescending(keySelector1)
               .OrderByDescending(keySelector2)
               .OrderByDescending(keySelector3);
          return this;
     }
}

So a way in which this can be used will be as follows:

var foos = _fooRepos.Fetch(
     f => f.Name == "two" || f.Name == "three",
     o => o.Asc(f => f.Name, f => f.Id)
);

Is this the best possible way of doing what I am trying to achieve? I am trying to make it as simple as possible. I would appreciate all help and if any sample code and articles.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:07:54+00:00Added an answer on May 27, 2026 at 1:07 am

    I’m sure the Orchard guys have their reasons (haven’t looked at the code base), but I’m just wondering what it brings to the table compared with a more ‘standard’ LINQ/IQueryable-based solution?

    public interface IRepository<T>  
    {  
        IQueryable<T> All(); 
    } 
    

    usage:

    var foos = from f in _foosRepos.All()
               where f.Name == "two" || f.Name == "three"
               orderby f.Name, f.Id;
    

    or

    var foos = _foosRepos.All()
            .Where(f => f.Name == "two" || f.Name == "three")
            .OrderBy(f => f.Name).ThenBy(f => f.Id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the repository pattern using entity framework code first rc 1.
I am using Entity Framework with the generic repository pattern. I have used the
I'm getting into using the Repository Pattern for data access with the Entity Framework
I'm trying to roll out a strategy pattern with entity framework and the repository
I am using Entity Framework v4 and the repository pattern found in the answer
i'm trying to implement a Repository and UnitOfWork patterns using Entity Framework. This is
I am trying to create a very generic generics repository for my Entity Framework
I'm trying to use the repository pattern to save an entity using the Entity
I'm using Entity Framework in my project, and I have the problem that, once
I am currently using ASP.NET MVC and Entity Framework Code First using the repository

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.