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 6709469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:53:08+00:00 2026-05-26T07:53:08+00:00

I am using EF. and the repository Pattern. The specific question is how can

  • 0

I am using EF. and the repository Pattern.

The specific question is how can I write the GetPositionByCustomExpression below so that from the caller I can pass an expression, sometimes the expression could contain one filter, or 2, or even the expression could contain an order?

     public interface IPositionRepository
        {
            void CreateNewPosition(Position contactToCreate);
            void DeletePosition(int id);
            Position GetPositionByID(int id);
            IEnumerable<Position> GetAllPositions();
            int SaveChanges();
            IEnumerable<Position> GetPositionByCustomExpression();  //---> What to put here??
        }


public class PositionRepository : IPositionRepository
    {

        private HRContext _db = new HRContext();

        public Position GetPositionByID(int id)
        {
            return _db.Positions.FirstOrDefault(d => d.PositionID == id);
        }

        public IEnumerable<Position> GetAllPosition()
        {
            return _db.Positions.ToList();
        }

        public void CreateNewContact(Position positionToCreate)
        {
            _db.Positions.Add(positionToCreate);
            _db.SaveChanges();
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }

        public void DeletePosition(int id)
        {
            var posToDel = GetPositionByID(id);
            _db.Positions.Remove(posToDel);
            _db.SaveChanges();
        }

        /// <summary>
        /// Lets suppose we have a field called name, another years of experience, and another department.
        /// How can I create a generic way in ONE simple method to allow the caller of this method to pass
        /// 1, 2 or 3 parameters.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Position> GetPositionByCustomExpression()   // --> What to put here?
        {
            _db.Positions.Find(); // ---> and here??

        }
  • 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-26T07:53:09+00:00Added an answer on May 26, 2026 at 7:53 am

    You can take a Expression<Func<Position, bool>> type parameter to the method

    public interface IPositionRepository
        {
            //others
            IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate);
        }
    
    public class PositionRepository : IPositionRepository
    {
    
        public IEnumerable<Position> GetPositionByCustomExpression(Expression<Func<Position, bool>> predicate)
        {
            _db.Positions.Where(predicate);
    
        }
     }
    

    Then you can use predicates as follows

    var positions = repository
    .GetPositionByCustomExpression(p => p.Name.StartsWith("Foo") && p.BasicSalary > 100000)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using the Repository pattern and I want to write a method that receives
I have implemented a DAL using Rob Conery's spin on the repository pattern (from
I knew than one of the benefits from using repository pattern make it easy
A question regarding the repository pattern and query object pattern. I'm using EF 4
I'm using the repository pattern. I have a Country repository that I'm using a
I am sortof using a repository pattern to extract information from a database. I
I am using a Repository pattern with filters in my MVC application. The project
I'm getting into using the Repository Pattern for data access with the Entity Framework
What is a typical approach to using the repository pattern with .NET 1.1 (C#)?
I'm working on a new project and I'm using the repository pattern, I have

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.