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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:04+00:00 2026-05-25T03:19:04+00:00

I’m trying to use Dapper simply to map my database tables to types in

  • 0

I’m trying to use Dapper simply to map my database tables to types in C#, however, some of my types need additional elements that are not in the table. To do this I am using a factory that can take column values and set the appropriate properties.

public IEnumerable<IMyType> All() {
  var query = _connection.Query("SELECT * FROM [table]");
  return query.Select(o => _myTypeFactory.Create(o));
}

Currently this is resulting the return statement generating an error:

Cannot convert expression type 'System.Collections.Generic.IEnumerable<dynamic>' to return type 'System.Collections.Generic.IEnumerable<IMyType>'

My factory class looks something like this:

public class MyTypeFactory {
  public IMyType Create(dynamic o) {
    return Create((String) o.Code, (Int32) o.KeyID);
  }
  public IMyType Create(String code, Int32 keyID) {
    return new MyType(code, Cache.Lookup(keyID));
  }
}

Why doesn’t the Select() method return IEnumerable<IMyType>? What do I need to do to make this work? Is this just the wrong approach and there’s a better way?

  • 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-25T03:19:05+00:00Added an answer on May 25, 2026 at 3:19 am

    The simplest fix is just to use the Cast<> LINQ operator:

    public IEnumerable<IMyType> All() {
      var query = _connection.Query("SELECT * FROM [table]");
      return query.Select(o => _myTypeFactory.Create(o))
                  .Cast<IMyType>();
    }
    

    Alternatively, you could cast each element:

    public IEnumerable<IMyType> All() {
      var query = _connection.Query("SELECT * FROM [table]");
      return query.Select(o => (IMyType) _myTypeFactory.Create(o));
    }
    

    It doesn’t currently work because there’s simply no implicit conversion available between IEnumerable<dynamic> and IEnumerable<IMyType>. IEnumerable<dynamic> could be implemented in any number of ways, and given that each item will be generated dynamically there’s no reason to suppose the result value will implement IEnumerable<IMyType>.

    I agree that it looks like the second form isn’t actually adding anything, but the compiler doesn’t check all the possible return types of _myTypeFactory.Create(o) – it treats that whole expression as a dynamic value, i.e. the expression is of type dynamic. Therefore the Select result is still of type IEnumerable<dynamic>.

    Another option is to specify the generic type argument to Select.

    public IEnumerable<IMyType> All() {
      var query = _connection.Query("SELECT * FROM [table]");
      return query.Select<IMyType>(o => _myTypeFactory.Create(o));
    }
    

    That’s attempting to force the lambda expression to a Func<dynamic, IMyType> – I believe that will work…

    EDIT: As noted in comments, forcing the method invocation to be resolved at compile-time will fix it too. Basically it depends what you find most readable.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.