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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:06:11+00:00 2026-05-26T12:06:11+00:00

I have recently been playing with Massive, a Micro-ORM which returns collections of IEnumerable<dynamic>.

  • 0

I have recently been playing with Massive, a Micro-ORM which returns collections of IEnumerable<dynamic>.

I discovered an unexpected issue when I tried to query one of those collections using LINQ.

While the compiler seems to have no issues whatsoever to work out that string.Format returns a string even when one of the arguments passed to it is declared as dynamic…

dynamic dynamicString = "d"; // just using a string here for simplicity, same problem occurs with any other type
string explicitString = string.Format("string is {0}", dynamicString); // works without issues

…it doesn’t appear to be able to infer that fact in the following scenario:

IEnumerable<string> strings = new[] { "a", "b", "c" };
IEnumerable<dynamic> dynamics = strings;

IEnumerable<string> output = dynamics.Select(d => string.Format("string is {0}", d)); // compiler error on this line

The compiler complains:

"Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<dynamic>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)"

Since the compiler should be able to infer that my lambda expression returns a string, I would have expected it to also infer that the TResult of the Select should be of type string (and not dynamic).

It was easy to fix by specifying the TSource and TResult explicitly like this:

IEnumerable<string> output2 = dynamics.Select<dynamic, string>(d => string.Format("string is {0}", d)); // works !!!

Or I could assign the result to an IEnumerable<dynamic>…

IEnumerable<dynamic> output3 = dynamics.Select(d => string.Format("string is {0}", d)); // also works

I have also confirmed that this problem does not occur when I replace my IEnumerable<dynamic> with an IEnumerable<object>:

IEnumerable<object> objects = strings;
IEnumerable<string> output4 = objects.Select(o => string.Format("string is {0}", o)); // works

And interestingly even the following works:

IEnumerable<string> output5 = dynamics.Select(d => string.Format("string is {0}", (object)d)); // works
IEnumerable<string> output6 = dynamics.Select(d => string.Format("string is {0}", (string)d)); // works

Can anybody explain what’s going on here? Is this a know limitation of the C# compiler or have I found yet another bug?

  • 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-26T12:06:12+00:00Added an answer on May 26, 2026 at 12:06 pm

    You need:

    IEnumerable<string> output = dynamics.Select(d => (string)string.Format(
           "string is {0}", d));
    

    It can’t infer the return type is string, because the dynamic means it has to assume the return is dynamic, in case there is a more suitable overload of string.Format for the specific type supplied (with a different return type). Even if we know otherwise, the spec for dynamic will disagree with us ;p By adding an explicit cast back to string we make the return type clear.

    Personally, I see no use for dynamic here; you might as well use object, then it isn’t an issue in the first place:

    IEnumerable<string> strings = new[] { "a", "b", "c" };
    IEnumerable<object> dynamics = strings;
    
    IEnumerable<string> output = dynamics.Select(d => string.Format(
          "string is {0}", d));
    

    (or indeed, leave as IEnumerable<string>) I assume you have some other reason to use dynamic that isn’t visible in this example.

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

Sidebar

Related Questions

Recently I've been playing a bit with Groovy and Grails and I have to
Recently I have started playing with jQuery, and have been following a couple of
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently been put in charge of debugging two different programs which will
Recently I have been playing around with the HTTP Proxy in twisted. After much
I have been playing around with php-gtk recently and in the past I have
I have recently been playing with the sqlite4java library. I think I have it
I have recently been playing around with Codeigniter to see what I can learn
I have been playing around in expect recently and I for the life of
I have been playing a web boardgame called Empires at GamesByEmail.com recently. It runs

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.