Linq Query Result As DataTable

Thread is closed for posting
5 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 17 May 2011 Link to this post


    Is it possible to convert LINQ query result into Datatable ... ? I was trying to do that but I am getting an exception.

    Linq Query :

     Dim query As IEnumerable(Of DataRow) = From Tariff In dbContext.FF_Tariffs
                          Select Tariff.JobID, Tariff.TariffName, Tariff.LockedDateTime

            Dim dt As DataTable = query.CopyToDataTable()

    Exception :

    Unable to cast object of type 'Telerik.OpenAccess.Query.Piece`1[VB$AnonymousType_0`3[System.Nullable`1[System.Int64],System.String,System.Nullable`1[System.DateTime]]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.
  2. Thomas
    Thomas avatar
    590 posts

    Posted 17 May 2011 Link to this post

    Hello Muhammad,

    at the moment this is not possible, though we might think about such an API in the future.
    What is your use case, and why would you map a strongly typed result to a datatable? 

    All the best,
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
  3. Allen
    Allen avatar
    42 posts
    Member since:
    Nov 2014

    Posted 20 Nov 2014 in reply to Thomas Link to this post

    First, in some occasions, we don't need to query all the columns of data.
    Second, we will sometimes multi table query.
    Normally only used in order to show the presentation layer.
    For the sake of convenience, we usually will not create a partial class, but use data tables.
    Using data tables allows us to save a lot of trouble.
  4. Doroteya
    Doroteya avatar
    498 posts

    Posted 24 Nov 2014 Link to this post

    Hello Zhen,

    Thank you for your feedback.

    In general, Telerik Data Access does not support such a scenario. 

    However, you can achieve the outcome you need (a custom subset of the columns of more than one table) with the help of anonymous types and LINQ projection. The idea is that you do not define a strongly typed class in your code and leave the compiler to encapsulate a set of properties into a single object. The properties in this case, would be the columns of the tables from which you select. Here is an example:
    using (EntitiesModel dbContext = new EntitiesModel())
        var query = (from c in dbContext.Cars
                        join cat in dbContext.Categories on c.CategoryID equals cat.CategoryID
                        select new
                            MyCategory = cat.CategoryName,
                            TagNumber = c.TagNumber,
                            Model = c.Model,
                            Make = c.Make
    Here, the type of the query variable is anonymous type and the projection is the subset of properties defined after the select new statement.

    I hope this helps. Let us know, if you need further information.

    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  5. Allen
    Allen avatar
    42 posts
    Member since:
    Nov 2014

    Posted 28 Nov 2014 in reply to Doroteya Link to this post

    I know. But I don't know if it can be used as a return value, outside use?
    Well, it actually worked.
    Thank you, Thomas
Back to Top