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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:25:16+00:00 2026-05-15T08:25:16+00:00

I’m trying to create a generic repository for my models. Currently i’ve 3 different

  • 0

I’m trying to create a generic repository for my models. Currently i’ve 3 different models which have no relationship between them. (Contacts, Notes, Reminders).

class Repository<T> where T:class
{
    public IQueryable<T> SearchExact(string keyword)
    {
        //Is there a way i can make the below line generic
        //return db.ContactModels.Where(i => i.Name == keyword)        
        //I also tried db.GetTable<T>().Where(i => i.Name == keyword)
        //But the variable i doesn't have the Name property since it would know it only in the runtime
        //db also has a method ITable GetTable(Type modelType) but don't think if that would help me
    }
}

In MainViewModel, I call the Search method like this:

Repository<ContactModel> _contactRepository = new Repository<ContactModel>();

public void Search(string keyword)
{
    var filteredList = _contactRepository.SearchExact(keyword).ToList();
}

Solution:

Finally went with Ray’s Dynamic Expression solution:

public IQueryable<TModel> SearchExact(string searchKeyword, string columnName)
{
    ParameterExpression param = Expression.Parameter(typeof(TModel), "i");
    Expression left = Expression.Property(param, typeof(TModel).GetProperty(columnName));
    Expression right = Expression.Constant(searchKeyword);
    Expression expr = Expression.Equal(left, right);
}

query = db.GetTable<TModel>().Where(Expression.Lambda<Func<TModel, bool>>(expr, param));
  • 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-15T08:25:16+00:00Added an answer on May 15, 2026 at 8:25 am

    Interface solution

    If you can add an interface to your object you can use that. For example you could define:

     public interface IName
     {
       string Name { get; }
     }
    

    Then your repository could be declared as:

    class Repository<T> where T:class, IName
    {
      public IQueryable<T> SearchExact(string keyword)  
      {  
        return db.GetTable<T>().Where(i => i.Name == keyword);
      }
    }  
    

    Alternate interface solution

    Alternatively you could put the “where” on your SearchExact method by using a second generic parameter:

    class Repository<T> where T:class
    {  
      public IQueryable<T> SearchExact<U>(string keyword) where U: T,IName
      {  
        return db.GetTable<U>().Where(i => i.Name == keyword);
      }
    }  
    

    This allows the Repository class to be used with objects that don’t implement IName, whereas the SearchExact method can only be used with objects that implement IName.

    Reflection solution

    If you can’t add an IName-like interface to your objects, you can use reflection instead:

    class Repository<T> where T:class
    {
      static PropertyInfo _nameProperty = typeof(T).GetProperty("Name");
    
      public IQueryable<T> SearchExact(string keyword)
      {
        return db.GetTable<T>().Where(i => (string)_nameProperty.GetValue(i) == keyword);
      }
    }
    

    This is slower than using an interface, but sometimes it is the only way.

    More notes on interface solution and why you might use it

    In your comment you mention that you can’t use an interface but don’t explain why. You say “Nothing in common is present in the three models. So i think making an interface out of them is not possible.” From your question I understood that all three models have a “Name” property. In that case, it is possible to implement an interface on all three. Just implement the interface as shown and “, IName” to each of your three class definitions. This will give you the best performance for both local queries and SQL generation.

    Even if the properties in question are not all called “Name”, you can still use the nterface solution by adding a “Name” property to each and having its getter and setter access the other property.

    Expression solution

    If the IName solution won’t work and you need the SQL conversion to work, you can do this by building your LINQ query using Expressions. This more work and is significantly less efficient for local use but will convert to SQL well. The code would be something like this:

    class Repository<T> where T:Class
    {
      public IQueryable<T> SearchExact(string keyword,
                                       Expression<Func<T,string>> getNameExpression)
      {
        var param = Expression.Parameter(typeof(T), "i");
        return db.GetTable<T>().Where(
                    Expression.Lambda<Func<T,bool>>(
                      Expression.Equal(
                        Expression.Invoke(
                          Expression.Constant(getNameExpression),
                          param),
                        Expression.Constant(keyword),
                      param));
      }
    }
    

    and it would be called thusly:

    repository.SearchExact("Text To Find", i => i.Name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker

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.