locked
Dynamic LINQ breaks on collections of type dynamic RRS feed

  • Question

  • I'm using LINQ for querying the database. I'm using dynamic keyword in queries. I don't know this dynamic mechanism in depth, so I don't understand what's going on. The situation follows. The following section of code:

    var qGroup = qLocalOrd.GroupBy("new(...)", "it");
    var qGroupCast = (qGroup as IQueryable<IGrouping<dynamic,dynamic>>).AsEnumerable();
    
    var qAgg = from ordg in qGroupCast
    select new {
    ordg.Key,
    Agg = (ordg.Key.OsID + " " + ordg.Key.NameOs + "\n" + 
    (ordg as IEnumerable<dynamic>).Aggregate("Составные части:\n", ...).Trim('\n') + ...)
    };

    Works just fine. But when I'm adding this to the end:

    var qPlain = qAgg.Cast<dynamic>().AsQueryable().Select("new(Key.SubSchet, Key.NameSubSchet, ...)");
    qPlain.Dump();

    I'm receiving an error like "No field "Key" exists in type "Object"". It's the same if I use

    (qAgg as IEnumerable<dynamic>)

    So at this point dynamic treatment of qAgg elements is broken somewhy.

    Why does this happen and how to make this thing work?

    P.S. Don't move this to LINQ to SQL branch, there are more active users here and this topic is relevant for LINQ to Entities as well.

    Wednesday, November 12, 2014 12:30 PM

All replies

  • I'm using LINQ for querying the database. I'm using dynamic keyword in queries. I don't know this dynamic mechanism in depth, so I don't understand what's going on. The situation follows. The following section of code:

    var qGroup = qLocalOrd.GroupBy("new(...)", "it");
    var qGroupCast = (qGroup as IQueryable<IGrouping<dynamic,dynamic>>).AsEnumerable();
    
    var qAgg = from ordg in qGroupCast
    select new {
    ordg.Key,
    Agg = (ordg.Key.OsID + " " + ordg.Key.NameOs + "\n" + 
    (ordg as IEnumerable<dynamic>).Aggregate("Составные части:\n", ...).Trim('\n') + ...)
    };

    Works just fine. But when I'm adding this to the end:

    var qPlain = qAgg.Cast<dynamic>().AsQueryable().Select("new(Key.SubSchet, Key.NameSubSchet, ...)");
    qPlain.Dump();

    I'm receiving an error like "No field "Key" exists in type "Object"". It's the same if I use

    (qAgg as IEnumerable<dynamic>)

    So at this point dynamic treatment of qAgg elements is broken somewhy.

    Why does this happen and how to make this thing work?

    • Merged by Fred Bao Thursday, November 13, 2014 5:20 AM Duplicated
    Wednesday, November 5, 2014 9:56 AM
  • Hello,

    What is the Dump() method? Is it a method of LINQ2SQL project?

    >>Why does this happen and how to make this thing work?

    I think that is becasue the api/library does not support this scenario. For a complex query, i usually place it to a stored produce which is faster and easier.

    Friday, November 7, 2014 8:08 AM
  • @ToBeFirst, Dump() is a method using in LINQPad, a very powerful c# scripting tool. I suggest you to check it out http://linqpad.net

    Friday, November 7, 2014 3:58 PM
  • I'm using LINQ for querying the database. I'm using dynamic keyword in queries. I don't know this dynamic mechanism in depth, so I don't understand what's going on. The situation follows. The following section of code:

    var qGroup = qLocalOrd.GroupBy("new(...)", "it");
    var qGroupCast = (qGroup as IQueryable<IGrouping<dynamic,dynamic>>).AsEnumerable();
    
    var qAgg = from ordg in qGroupCast
    select new {
    ordg.Key,
    Agg = (ordg.Key.OsID + " " + ordg.Key.NameOs + "\n" + 
    (ordg as IEnumerable<dynamic>).Aggregate("Составные части:\n", ...).Trim('\n') + ...)
    };

    Works just fine. But when I'm adding this to the end:

    var qPlain = qAgg.Cast<dynamic>().AsQueryable().Select("new(Key.SubSchet, Key.NameSubSchet, ...)");
    qPlain.Dump();

    I'm receiving an error like "No field "Key" exists in type "Object"". It's the same if I use

    (qAgg as IEnumerable<dynamic>)

    So at this point dynamic treatment of qAgg elements is broken somewhy.

    Why does this happen and how to make this thing work?

    P.S. Don't move this to LINQ to SQL branch, there are more active users here and this topic is relevant for LINQ to Entities as well.

    Hello, any news about this thread? https://social.msdn.microsoft.com/Forums/en-US/f2ac7366-3294-4ca9-84c3-1038e9116e92/dynamic-linq-issues?forum=linqtosql
    Wednesday, November 12, 2014 12:56 PM
  • I'm using LINQ for querying the database. I'm using dynamic keyword in queries. I don't know this dynamic mechanism in depth, so I don't understand what's going on. The situation follows. The following section of code:

    var qGroup = qLocalOrd.GroupBy("new(...)", "it");
    var qGroupCast = (qGroup as IQueryable<IGrouping<dynamic,dynamic>>).AsEnumerable();
    
    var qAgg = from ordg in qGroupCast
    select new {
    ordg.Key,
    Agg = (ordg.Key.OsID + " " + ordg.Key.NameOs + "\n" + 
    (ordg as IEnumerable<dynamic>).Aggregate("Составные части:\n", ...).Trim('\n') + ...)
    };

    Works just fine. But when I'm adding this to the end:

    var qPlain = qAgg.Cast<dynamic>().AsQueryable().Select("new(Key.SubSchet, Key.NameSubSchet, ...)");
    qPlain.Dump();

    I'm receiving an error like "No field "Key" exists in type "Object"". It's the same if I use

    (qAgg as IEnumerable<dynamic>)

    So at this point dynamic treatment of qAgg elements is broken somewhy.

    Why does this happen and how to make this thing work?

    P.S. Don't move this to LINQ to SQL branch, there are more active users here and this topic is relevant for LINQ to Entities as well.

    Hello, any news about this thread? https://social.msdn.microsoft.com/Forums/en-US/f2ac7366-3294-4ca9-84c3-1038e9116e92/dynamic-linq-issues?forum=linqtosql

    Hello, any news about this thread? https://social.msdn.microsoft.com/Forums/en-US/f2ac7366-3294-4ca9-84c3-1038e9116e92/dynamic-linq-issues?forum=linqtosql

    Moderator, where are you?

    Tuesday, November 25, 2014 7:30 PM