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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:03:08+00:00 2026-05-31T15:03:08+00:00

I am trying to build a controller that services edit requests for all my

  • 0

I am trying to build a controller that services edit requests for all my lookup tables. I have several DbSet variables on my DbContext that derive from IdNamePairBase, e.g:

public DbSet<Country> Countries { get; set; } // Country derives from IdNamePairBase

How can I pass the name of one of these to a query, to get all items in that list? E.g.

var list = db.GetNamedDbSet("Countries");

Then, for bonus points I need to get an IEnumerable<IdNamePairBase> from list.

  • 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-31T15:03:09+00:00Added an answer on May 31, 2026 at 3:03 pm

    You could use the Set(Type type) on the DbContext if the name of the table corresponds to the type.

     public IEnumerable<IdNamePairBase> GetNamedDbSet(string dbSetName)
     {
          var property = Type.GetType(dbSetName);
          if (property == null || !property.CanRead)
          {
             throw new ArgumentException("DbSet named " + dbSetName + " does not exist." );
          }
    
          // at this point you might want to check that the property is an enumerable type
          // and that the generic definition is convertible to IdNamePairBase by
          // inspecting the property type.  If you're sure that these hold, you could
          // omit the check.
    
          return Set(type).Cast<IdNamePairBase>();
     }
    

    Original Answer

    If the name of the set matches the property name, you can use reflection.

     public IEnumerable<IdNamePairBase> GetNamedDbSet( string dbSetName )
     {
          var property = this.GetType().GetProperty( dbSetName );
          if (property == null || !property.CanRead)
          {
             throw new ArgumentException("DbSet named " + dbSetName + " does not exist." );
          }
    
          // at this point you might want to check that the property is an enumerable type
          // and that the generic definition is convertible to IdNamePairBase by
          // inspecting the property type.  If you're sure that these hold, you could
          // omit the check.
    
          var result = new List<IdNamePairBase>();
          foreach (var item in (IEnumerable)property.GetValue( this, null))
          {
              result.Add( (IdNamePairBase)item );
          }
          return result;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a form that will list all users and allow you
I am trying to build a program using model-view-controller. Specifically: I have a state
I am trying to build an application that can search the database for different
I'm trying to build a pager where it's just Next / Previous buttons that
I am trying to build a route with the format of {controller}{action}{classificationID}{reviewID}{CategoryID}\ but I
I'm trying to build application that use single config passed by server as non
Currently, I'm trying to build a game with UIkit that consists of the three
I'm working on a small picture application. That I'm trying to do is build
I am trying to build an iPhone application that has a Tab bar as
I'm trying to build a really simple php controller page for a small site.

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.