locked
How to convert IEnumerable of type anonymous to type datarow RRS feed

  • Question

  • Hello,

    I have this linq query that returns a IEnumerable of type  anonymous, how do I convert it to IEnumerable of type datarow or my the query return IEnumerable of type datarow.

     var data = (CurrentLog.Tables[0].AsEnumerable().Where(
                        row => row.Field<int>("ApplicationId") == _applicationId).Select(row => new
                                                                                                    {
                                                                                                        ID =
                                                                                                    row.Field<int>("userid"),
                                                                                                        NAME =
                                                                                                    row.Field<string>("username")
                                                                                                    })).OrderBy(a => a.NAME);

    Thanks.
    • Edited by capiono Friday, August 10, 2012 5:16 PM
    Friday, August 10, 2012 5:08 PM

Answers

  • You can't use Cast to convert some type to some other type.  It'll fail at runtime with a typecast exception.

    The issue is that you can't just convert an anonymous type to a DataRow because the anony type can't be directly referenced.  What you should do is either create the DataRow directly in your query or return a custom type that wraps the values.  Personally I'd just use Tuple<int, string> to return the values from the query.  But this begs the question, what type is CurrentLog.Tables[0].  If it is a DataTable then you already have the DataRow elements so you can just do this:

    from r in CurrentLog.Tables[0].Rows
    where r.Field<int>("ApplicationId") == _applicationId)
    select r;

    That returns IEnumerable<DataRow>.  If Tables[0] is some other type then you'll have to hydrate the DataRow.  The problem is that DataRow is not creatable.  You either have to use a DataTable to create it or, if you're using a strongly typed dataset, then use the strongly typed DataRow type.

    Michael Taylor - 8/10/2012
    http://msmvps.com/blogs/p3net

    • Proposed as answer by Lisa Zhu Monday, August 13, 2012 9:46 AM
    • Marked as answer by capiono Monday, August 13, 2012 4:02 PM
    Friday, August 10, 2012 7:10 PM

All replies

  • You can either not do the select in the first place to turn it from a data row to an anonymous type, you can manually create a new data row, rather than a new anonymous type, or you can use the code provided on this MSDN page.  The first option seems to be the best in this case.
    Friday, August 10, 2012 5:54 PM
  • Hi,

        u can use the cast function.

     var data = (CurrentLog.Tables[0].AsEnumerable().Where(
                        row => row.Field<int>("ApplicationId") == _applicationId).Select(row => new
                                                                                                    {
                                                                                                        ID =
                                                                                                    row.Field<int>("userid"),
                                                                                                        NAME =
                                                                                                    row.Field<string>("username")
                                                                                                    })).OrderBy(a => a.NAME).cast<yourtype>();

    Thanks,

    Sanjay.

    • Marked as answer by capiono Friday, August 10, 2012 6:04 PM
    • Unmarked as answer by capiono Friday, August 10, 2012 6:15 PM
    Friday, August 10, 2012 6:03 PM
  •  var data = (CurrentLog.Tables[0].AsEnumerable().Where(
                        row => row.Field<int>("ApplicationId") == _applicationId).Select(row => new
                                                                                                    {
                                                                                                        ID =
                                                                                                    row.Field<int>("userid"),
                                                                                                        NAME =
                                                                                                    row.Field<string>("username")
                                                                                                    })).OrderBy(a => a.NAME).ToEnumerable();
    • Proposed as answer by kishhr Saturday, August 11, 2012 6:47 PM
    Friday, August 10, 2012 6:23 PM
  • Hi,

        u can use the cast function.

     var data = (CurrentLog.Tables[0].AsEnumerable().Where(
                        row => row.Field<int>("ApplicationId") == _applicationId).Select(row => new
                                                                                                    {
                                                                                                        ID =
                                                                                                    row.Field<int>("userid"),
                                                                                                        NAME =
                                                                                                    row.Field<string>("username")
                                                                                                    })).OrderBy(a => a.NAME).cast<yourtype>();

    Thanks,

    Sanjay.

    Cast only helps if the type is actually whatever you want it to be, in this case a DataRow.  It's not here (it's an anonymous type) so it will just crash at runtime.
    Friday, August 10, 2012 7:09 PM
  •  var data = (CurrentLog.Tables[0].AsEnumerable().Where(
                        row => row.Field<int>("ApplicationId") == _applicationId).Select(row => new
                                                                                                    {
                                                                                                        ID =
                                                                                                    row.Field<int>("userid"),
                                                                                                        NAME =
                                                                                                    row.Field<string>("username")
                                                                                                    })).OrderBy(a => a.NAME).ToEnumerable();
    That just doesn't do anything at all.  It will act identically to the OP's code.  The ToEnumerable, in this particular context, is just a no-op.
    Friday, August 10, 2012 7:10 PM
  • You can't use Cast to convert some type to some other type.  It'll fail at runtime with a typecast exception.

    The issue is that you can't just convert an anonymous type to a DataRow because the anony type can't be directly referenced.  What you should do is either create the DataRow directly in your query or return a custom type that wraps the values.  Personally I'd just use Tuple<int, string> to return the values from the query.  But this begs the question, what type is CurrentLog.Tables[0].  If it is a DataTable then you already have the DataRow elements so you can just do this:

    from r in CurrentLog.Tables[0].Rows
    where r.Field<int>("ApplicationId") == _applicationId)
    select r;

    That returns IEnumerable<DataRow>.  If Tables[0] is some other type then you'll have to hydrate the DataRow.  The problem is that DataRow is not creatable.  You either have to use a DataTable to create it or, if you're using a strongly typed dataset, then use the strongly typed DataRow type.

    Michael Taylor - 8/10/2012
    http://msmvps.com/blogs/p3net

    • Proposed as answer by Lisa Zhu Monday, August 13, 2012 9:46 AM
    • Marked as answer by capiono Monday, August 13, 2012 4:02 PM
    Friday, August 10, 2012 7:10 PM