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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:16:58+00:00 2026-05-23T20:16:58+00:00

How can I achieve something like the following? public interface IGenericRepository { int id

  • 0

How can I achieve something like the following?

public interface IGenericRepository 
{
    int id { get; }

    T GetById<T>() where T : class
}

public class GenericRepository : IGenericRepository 
{
    //Some code here

    public T GetById<T>(int tid) where T : class 
    { 
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
 }

And I would like to use this as follows:

GenericRepository gr = new GenericRepository();
Category cat = gr.GetById<Category>(15);

Of course, in this usage, tbl.id in the GenericRepository gives me an error.

SOLUTION

public class IHasId
{
    public int id { get; set; }
}

public interface IGenericRepository
{
    int id { get; }

    T GetById<T>(int id) where T : IHasId;
}

public class GenericRepository : IGenericRepository
{
    public int id
    {
        get { throw new NotImplementedException(); }
    } 

    public T GetById<T>(int id) where T : IHasId
    {
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
}

And apart from these, DON’T forget to define this somewhere in your model:

public partial class Category : IHasId { }

And the usage is:

Repository rep = new Repository();
Category cat = rep.GetById<Category>(15);
  • 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-23T20:16:58+00:00Added an answer on May 23, 2026 at 8:16 pm
    public class IHasId
    {
        public int id { get; set; }
    }
    
    public interface IGenericRepository<T>
    {
        int id { get; }
    
        T GetById(int id);
    }
    
    public class GenericRepository<T> : IGenericRepository<T> where T : IHasId
    {
        public int id
        {
            get { throw new NotImplementedException(); }
        }
    
        public T GetById(int id)
        {
            return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have something like the following: class C { public Foo
I want to achieve something like the following: Where I can select multiple folders
I have the following code and want to achieve something like the below demonstrated
How can I achieve something simple like the following without creating a new css
In C++, you can do the following: class base_class { public: virtual void do_something()
My application has the following classes: public class Widget { public virtual int Id
Long story short Say I have the following code: // a class like this
I have the following action: public class HomeController : Controller { public ActionResult Index(int?
I have the following interfaces defined: public interface IAudit { DateTime DateCreated { get;
how can i paste something on multiple lines? i'm trying to achieve the same

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.