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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:15:20+00:00 2026-05-30T05:15:20+00:00

I have a question about generic list. Assume that I have a database which

  • 0

I have a question about generic list.

Assume that I have a database which contains three tables, User table, Order table and Item table.

In the C# code, I want a function, called GetList to get all he records from one of the three tables.

If I don’t use generic method, I have to create 3 different methods which looks like GetAllUsers(), GetAllOrders() and GetAllItems(). Inside these 3 methods, I will have to write the linq to query the database.

My question is how do I implement a generic method.

I want to use EF 4.1. I know how to do it by using NHibrate tho.

UPDATE

What about this? I found it from another post, it can be used to get one single record

        public T GetSingleEntity<T>(Expression<Func<T,bool>> expression) where T:class
    {
        using (PersonalLinksEntities dbContext = new PersonalLinksEntities()) 
        {
            return dbContext.CreateObjectSet<T>().SingleOrDefault(expression);
        }

    }

PersonalLinksEntities is generated by EF, it is database first model.

Here is how I call this method

    public ActionResult Index()
    {
        var thisOne = base.GetSingleEntity<PersonalRecord>(r=>r.RecordID==1);            
        return View();
    }
  • 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-30T05:15:21+00:00Added an answer on May 30, 2026 at 5:15 am

    The specific implementation going to depend on what you are using to access your data. NHib, EF, L2S? These all have some way of accessing an IQueryable generically.

    You could expose a method such as:

    public IQueryable<T> GetQueryable<T>()
    {
       //Implementation depends on your data provider
       //return dataContext.GetTable<T>();
       //return session.Query<T>();
    }
    

    But probably what you want to do is follow a repository pattern with a different repository for each type. But you can use an abstract base repository that accepts a type parameter and is reusable.

    public abstract class RepositoryBase<T>
    {
       public IQueryable<T> GetQuery()
       {
         //Implementation determined by provider
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two generic list objects, in which one contains ids and ordering, and
I have a question about following variable declaration. What does that mean? List<string>.Enumerator enumerator
I have a Java question about generics. I declared a generic list: List<? extends
I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1:
I have a (simple) question about generic classes in c++. I have some knowledge
this is a generic question about a facebook application creation. I want the user
the title might be too generic, but I have a very specific question about
When speaking about attribute lists I mean a generic list which stores additional information
I have a question about generics. I have a function that knows about a
I have a couple of questions about generic wildcards in Java: What is the

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.