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

  • Home
  • SEARCH
  • 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 888891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:28:38+00:00 2026-05-15T13:28:38+00:00

I’m trying to write a generic repository for my Entity Framework based application. Here’s

  • 0

I’m trying to write a generic repository for my Entity Framework based application. Here’s my prototype code:

Interface

public interface IDomainRepository
    {
        T GetById<T>(int id, Expression<Action<T>> idx)
    }

and the repository:

public class DomainRepository : IDomainRepository
    {
        private readonly DatabaseDataContext _ctx;

        public DomainRepository(DatabaseDataContext ctx)
        {
            _ctx = ctx;
        }

        public T GetById<T>(int id, Expression<Action<T>> idx)
        {
            return _ctx.GetTable(typeof (T)).SingleOrDefault(idx);
        }
    }

The above is test code which doesn’t work. But what I’d like to be able to do is this:

var repository = new DomainRepository(myContext);

var client = repository.GetById<tbl_Clients>(23, c => c.clientId);

So basically I want to get a client entity from the database by passing in the id plus a lambda telling GetById what the id column is. Also, I have no idea how I would execute the lambda using the passed id.

Can someone help me with this?

EDIT:

I’m really close. I’ve changed GetById:

public T GetById<T>(int id, Expression<Func<T, object>> idx)

and I can now call it like this:

var g = repository.GetById<tbl_Client>(23, c => c.cl_id);

but I don’t know how to use idx and check it’s value against the passed id:

 public T GetById<T>(int id, Expression<Func<T, object>> idx)
        {
            //var col = idx.Compile().Invoke(T);
           // How do I check if the column passed to "idx" is equal to id?

            return default(T);
        }

EDIT:
Think I got this working now. Here is my entire code, plus test:

public interface IDomainRepository
    {
        T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class;

        IEnumerable<T> GetAll<T>() where T : class;
        IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class;
        IEnumerable<T> Query<T>(ISpecification<T> filter) where T : class;

        void Add<T>(T entity) where T : class;
        void Delete<T>(T entity) where T : class;
        Table<T> GetTable<T>() where T : class;
    }

public class DomainRepository : IDomainRepository
    {
        private readonly DatabaseDataContext _ctx;

        public DomainRepository(DatabaseDataContext ctx)
        {
            _ctx = ctx;
        }

        public T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class
        {
            return (from i in GetAll<T>()
                    let h = idx.Compile().Invoke(i)
                    where Convert.ToInt32(h) == id
                    select i).SingleOrDefault();
        }

        public IEnumerable<T> GetAll<T>() where T : class
        {
            return GetTable<T>().ToList();
        }

        public IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class
        {
            return GetTable<T>().Where(filter);
        }

        public IEnumerable<T> Query<T> (ISpecification<T> filter) where T : class
        {
            return GetTable<T>().Where(filter.Predicate);
        }

        public void Add<T> (T entity) where T : class
        {
            GetTable<T>().InsertOnSubmit(entity);
        }

        public void Delete<T> (T entity) where T : class
        {
            GetTable<T>().DeleteOnSubmit(entity);
        }

        public Table<T> GetTable<T>() where T : class
        {
            return _ctx.GetTable(typeof(T)) as Table<T>;
        }
    }


var repository = new DomainRepository(_ctx);

var g = repository.GetById<tbl_Client>(1, c => c.cl_id);

I’ll keep testing this to see if it’s OK.

Cheers.
Jas.

  • 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-15T13:28:38+00:00Added an answer on May 15, 2026 at 1:28 pm

    OK, I think I have the final version, will need a bit more testing though:

    public T GetById<T>(int id, Func<T, int> idx) where T : class
            {
                return (from i in GetAll<T>()
                        where idx(i) == id
                        select i).SingleOrDefault();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So I've found the issue. Essentially for all the elements… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer The block of code that I was missing was the… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer Yes. This talks about it http://sixrevisions.com/web_design/the-960-grid-system-made-easy/?utm_medium=twitter&utm_source=twitterfeed (see "The Beginning and… May 15, 2026 at 7:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.