none
C# 4.0 support dynamic key word So Why we are not able to use dynamic result set in Linq query? RRS feed

  • Question

  • C# 4.0 support dynamic key word So Why we are not able to use dynamic result set in Linq query?

    • Edited by IamCkNitin Wednesday, December 19, 2012 10:10 AM
    Wednesday, December 19, 2012 10:08 AM

Answers

  • The compiler cannot know how the query must be processed. In that sample, if it allowed queries on dynamic sources, the query would be translated into

    var VarResult = resultSet.Select(v => v);


    which would not be compilable because there's no way to infer the type of v. Let's suppose the compiler does something clever like casting the lambda expression to a Func<dynamic,dynamic>:

    var VarResult = resultSet.Select((Func<dynamic, dynamic>)(v => v));

    And now, what happens at runtime? The Object class doesn't have a Select method. You get a RuntimeBinderException. In other words: you can only uses sources for which there is an available Select method known at compile-time.

    You need to help the compiler. For example, by specifying that resultSet is an IEnumerable<dynamic>:

    var VarResult = from v in (IEnumerable<dynamic>)resultSet select v;
    • Edited by Louis.fr Wednesday, December 19, 2012 2:13 PM
    • Marked as answer by IamCkNitin Thursday, December 20, 2012 2:32 AM
    Wednesday, December 19, 2012 2:12 PM
  • Hi,

    The purpose of Linq is to query data in a strongly typed manner (you could still use a good old DataReader or DataSet or whatever if you want to run arbitrary queries with design time unkown fields). Or could it be some confusion with anonymous types ? Else try to clarify what you are trying to do...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    • Edited by Patrice ScribeMVP Wednesday, December 19, 2012 10:27 AM
    • Marked as answer by IamCkNitin Wednesday, December 19, 2012 10:32 AM
    • Unmarked as answer by IamCkNitin Wednesday, December 19, 2012 10:37 AM
    • Marked as answer by IamCkNitin Thursday, December 20, 2012 2:33 AM
    Wednesday, December 19, 2012 10:27 AM

All replies

  • It's not clear what you're saying cannot be done. Could you explain it, or show an example of what you'd like to do but can't?

    Wednesday, December 19, 2012 10:22 AM
  • Hi,

    The purpose of Linq is to query data in a strongly typed manner (you could still use a good old DataReader or DataSet or whatever if you want to run arbitrary queries with design time unkown fields). Or could it be some confusion with anonymous types ? Else try to clarify what you are trying to do...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    • Edited by Patrice ScribeMVP Wednesday, December 19, 2012 10:27 AM
    • Marked as answer by IamCkNitin Wednesday, December 19, 2012 10:32 AM
    • Unmarked as answer by IamCkNitin Wednesday, December 19, 2012 10:37 AM
    • Marked as answer by IamCkNitin Thursday, December 20, 2012 2:33 AM
    Wednesday, December 19, 2012 10:27 AM
  • Hi

    Here is the code

    string[] PersonName = { "Joe", "Mac", "Torry", "Marsh", "Veronica"};

    dynamic resultSet = from p in PersonName select p;

    var VarResult = from v in resultSet select v ; (Compiler error in this line)

    Thanks



    • Edited by IamCkNitin Wednesday, December 19, 2012 10:31 AM
    Wednesday, December 19, 2012 10:30 AM
  • A Linq expression is compiled as calls to method extensions for IEnumerable, IEnumerable<T> (to ensure the source is enumerable). It seems they just disallowed generating code when you can't even guarantee you'll actually work against a source that is indeed enumerable.

    The real question is rather why you would need this. Why can't you just go for var resultSet=from p in PersonName select p; as you have done in the second line ? What is your goal in using dynamic rather than var ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Proposed as answer by JMCF125 Wednesday, December 19, 2012 1:23 PM
    Wednesday, December 19, 2012 12:43 PM
  • The compiler cannot know how the query must be processed. In that sample, if it allowed queries on dynamic sources, the query would be translated into

    var VarResult = resultSet.Select(v => v);


    which would not be compilable because there's no way to infer the type of v. Let's suppose the compiler does something clever like casting the lambda expression to a Func<dynamic,dynamic>:

    var VarResult = resultSet.Select((Func<dynamic, dynamic>)(v => v));

    And now, what happens at runtime? The Object class doesn't have a Select method. You get a RuntimeBinderException. In other words: you can only uses sources for which there is an available Select method known at compile-time.

    You need to help the compiler. For example, by specifying that resultSet is an IEnumerable<dynamic>:

    var VarResult = from v in (IEnumerable<dynamic>)resultSet select v;
    • Edited by Louis.fr Wednesday, December 19, 2012 2:13 PM
    • Marked as answer by IamCkNitin Thursday, December 20, 2012 2:32 AM
    Wednesday, December 19, 2012 2:12 PM
  • Many thanks for your answer.
    Thursday, December 20, 2012 2:31 AM
  • Many thanks for your answer.
    Thursday, December 20, 2012 2:32 AM