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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:24:03+00:00 2026-06-18T02:24:03+00:00

In my application I use Dapper to access the database. I’ve the following example

  • 0

In my application I use Dapper to access the database.

I’ve the following example code:

public IEnumerable GetByParentId(Type childType, string table) 
{
    IDbConnection _connection = _dbProvider.GetConnection();

    var _sqlString = "select * from " + table;

    IEnumerable _ret = _connection.Query(_sqlString).ToList(); 
    //return IEnumerable<Dapper.SqlMapper.FastExpando> 

    return _ret;
}

It is possible to cast FastExpando item to my childType or force Dapper to return a strongly typed collection?

I cannot change my method signature!

  • 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-18T02:24:04+00:00Added an answer on June 18, 2026 at 2:24 am

    You can call Query method via reflection and supply generic TItem argument of your childType. Then, Dapper will return IEnumerable and you will be able to cast it.
    Also, you could fork Dapper (it is not very big) and make a very simple overload of Query, that will include (Type childType) argument and call proper methods inside.

    What you face is the problem of C# in working with generics. Being statically typed language, C# is bad a working with dynamic types. If you want to work dynamically, you always end up with reflection.

    Here’s a sample of how you could call query method with type arguments. You may have to correct this a bit:

        public IEnumerable GetByParentId(Type childType, string table)
        {
            IDbConnection _connection = _dbProvider.GetConnection();
    
            var _sqlString = "select * from " + table;
    
            var t = typeof(SqlMapper);
            var genericQuery = t.GetMethods().Where(x => x.Name == "Query" && x.GetGenericArguments().Length == 1).First(); // You can cache this object.
            var concreteQuery = genericQuery.MakeGenericMethod(childType); // you can also keep a dictionary of these, for speed.
            var _ret = (IEnumerable)concreteQuery.Invoke(null, new object[] { _connection, _sqlString });
    
            return _ret;
        }
    

    Append:

    Also, I see a more general design problem here. You want to specify type dynamically, but then want to get statically typed objects, that you will be able to cast (I assume statically, or you want to continue with reflection?). Then… Why do you create dynamic interface in the first place? You say, that you can’t change the interface, but this looks a bit stupid. It seems, that all your context is statically typed, but then, for some reason, you have one dynamically typed method.
    If you know types during compilation time (or via generic arguments in the runtime), then you should simply change your method to something like this:

        public IEnumerable<T> GetByParentId<T>(string table)
        {
            IDbConnection _connection = _dbProvider.GetConnection();
            var _sqlString = "select * from " + table;
            var _ret = _connection.Query<T>(_sqlString);
            return _ret;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has be entity model as below and use Dapper public class Goal
Somebody said that when your PHP code and application use global variables then it
Based on the following example URL structure: mysite.com/mypage.aspx?a=red&b=green&c=blue Pages in the application use ASP.net
I push my application(use mysql service) to cloudfoundry success.and use vmc tunnel access to
I write code primarily for personal use, but I'm considering releasing an application (scientific
I am trying to use Dapper support my data access for my server app.
Can the application use set same char in the XON and XOFF? If yes,
I'm using Visual Studio to create my installation package. My application use some bitmap
I use application property to count number of online users: Application[OnlineUsers] = (int)Application[OnlineUsers] +
An application I use, Mozy Backup , adds its own drive to Windows Explorer

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.