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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:11:55+00:00 2026-06-04T13:11:55+00:00

I have 4 tables in the DB, each one has Id and Name, but

  • 0

I have 4 tables in the DB, each one has Id and Name, but they represent different things.
for each “thing” i have a different class, they all inherit from “thing”.
i have 4 functions :

List<thing1> getAllThings1();
List<thing2> getAllThings2();
List<thing3> getAllThings3();
List<thing4> getAllThings4();

each function reads from different table and creates the needed list.

Because i want to avoid code duplication i wanted to make a utility function that receives the table name (as string) and the type (thing1, thing2 … etc), and returns List<t>.

unfortunately this is impossible (without reflection) :
Create list of variable type

My current solution is that i have a function that returns a List, i call her in each “getAllThings#” and then manually convert each “thing” in the list to the proper thing by using ConvertAll and passing to him some converter.

I don’t like this solution, it feels wrong because i create a list and create a new one. very inefficient.
is there a better way to do it ?

thanks

  • 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-04T13:11:57+00:00Added an answer on June 4, 2026 at 1:11 pm

    Why not use generics?

    public IList<T> GetAllThings<T>(string tableName) where T : Thing {
        return new List<T>();
    }
    

    You will be able to call it

    IList<Thing4> things4 = thingProvider.GetAllThing<Thing4>( "thing4Table" );
    

    you could also have a Dictionary for storing the table name for each type, so you don’t have to provide the table name to the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2(or more) tables with different ids and class names within each tables
I have a table column where each row has one of three states, and
I have a ms access database that has one table for each photo album
I have this schema: There is a table for persons. Each person has one
I have a table with one column and four rows. Each tr has a
i have two tables products and reviews each product has several reviews linked by
I have three tables with 5 columns each. They can grow upto 15 rows
I'm using Access 2010. I have a database with multiple tables, each containing different
I have a two tables. One has manufacturer information and includes the regions where
I have a class CommonTableModel that has several instance methods and each operate on

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.