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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:52:36+00:00 2026-06-16T01:52:36+00:00

Let me start with, I am not sure if this is possible. I am

  • 0

Let me start with, I am not sure if this is possible. I am learning generics and I have several repositories in my app. I am trying to make an Interface that takes a generic type and converts it to something that all of the repositories can inherit from. Now on to my question.

public interface IRepository<T>
{
    IEnumerable<T> FindAll();
    IEnumerable<T> FindById(int id);
    IEnumerable<T> FindBy<A>(A type);
}

Is it possible to use a generic to determine what to find by?

public IEnumerable<SomeClass> FindBy<A>(A type)
{
    return _context.Set<SomeClass>().Where(x => x. == type); // I was hoping to do x.type and it would use the same variable to search.
}

To clarify a little better I was considering to be a string, int or whatever type I wanted to search for. What I am hoping for is I can say x.something where the something is equal to the variable passed in.

I can set any repository to my dbcontext using the

public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
    return base.Set<TEntity>();
}

Any Suggestions?

  • 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-06-16T01:52:37+00:00Added an answer on June 16, 2026 at 1:52 am

    If you use Expression<Func<T, bool>> instead of A like this:

    public interface IRepository<T>
    {
        ... // other methods
        IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate);
    }
    

    You can query the type using linq and specify the query in the code which calls the repository class.

    public IEnumerable<SomeClass> FindBy(Expression<Func<SomeClass, bool>> predicate)
    {
        return _context.Set<SomeClass>().Where(predicate);
    }
    

    And call it like this:

    var results = repository.FindBy(x => x.Name == "Foo");
    

    And given that it’s a generic expression, you don’t have to implement it in each repository, you can have it in the generic base repository.

    public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
    {
        return _context.Set<T>().Where(predicate);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start by saying im not 100% the way Im handling this is
Let’s start with this statement: We have published couple of free applications on the
Let me start by saying that this app was working fine the day before.
Not sure whether this is the right place, but I have a question related
I'm not sure if this is at least possible, but I want to access
I'm not sure if this is possible, or if it is an acceptable/unacceptable practice,
I'm not even sure of the right terminology, so let me start with my
I'm not sure if anything like this is even possible, but I was told
I am not even sure if this is even possible or how easy it
Let me start out by saying that I'm not a C developer and I

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.