none
linq.dynamic.core join resultSelector InvalidCastException RRS feed

  • Question

  • I'm trying to join two DataRow Collections on a dynamic Field.

    This is the query.

    IQueryable<DataRow> dataRows1= dsGroupValues.Tables[0].AsEnumerable().AsQueryable();
    IQueryable<DataRow> dataRows2 = dsGroupValues1.Tables[0].AsEnumerable().AsQueryable();string strKeyField="Campaign";
    var result = dataRows1.Join(dataRows2,
                               "new(it[strKeyField] as Key)",
                               "new(it[strKeyField] as Key)",
                               "new(outer as outTable, inner as innTable)");

    But I get the below error:

    System.InvalidCastException: 'Unable to cast Object of type 'System.Linq.EnumerableQuery1[<>f__AnonymousType11[System.Data.DataRow]]' to type 'System.Linq.IQueryable`1[System.Data.DataRow]'.

    Monday, January 21, 2019 6:20 PM

All replies

  • Hi AndreaFil,

    It seems that pass the incorrect arguments into Join method, the following code for your reference.

    IQueryable<DataRow> dataRows1 = dsGroupValues.Tables[0]..AsEnumerable().AsQueryable();
    IQueryable<DataRow> dataRows2 = dsGroupValues1.Tables[0]..AsEnumerable().AsQueryable();
    string strKeyField = "Campaign";
    var result = dataRows1.Join(dataRows2,
                                dr1 => dr1.Field<string>(strKeyField),
                                dr2 => dr2.Field<string>(strKeyField),
                                (dr1,dr2) => new {
                                               outer = dr1,
                                               inner = dr2
    });

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 22, 2019 1:38 AM
  • Thanks Zhanglong,
    but the problem is that the number of key fields may vary.

    So I'm using System.Linq.Dynamic.Core library

      https://github.com/StefH/System.Linq.Dynamic.Core

      https://stefh.github.io/System.Linq.Dynamic.Core/html/2e4513d7-4001-9937-6ff2-c88404490299.htm

    that let me use strings as join fields.


    I'm trying with just 1 field. When I find the solution, I will add the others.


    • Edited by AndreaFil Tuesday, January 22, 2019 7:52 AM
    Tuesday, January 22, 2019 7:49 AM
  • Hi AndreaFil,

    According to your description, it seems that System.Linq.Dynamic.Core is a 3rd-party library, I would suggest that you could post your issue github with System.Linq.Dynamic.Core for suitable support.

    https://github.com/StefH/System.Linq.Dynamic.Core/issues

    Thanks for your understanding.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 23, 2019 1:14 AM