This is a migrated thread and some comments may be shown as answers.

Linq Query Result As DataTable

4 Answers 514 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Muhammad
Top achievements
Rank 1
Muhammad asked on 17 May 2011, 11:20 AM
Hi,

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]'.

4 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 17 May 2011, 03:37 PM
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,
Thomas
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
Allen
Top achievements
Rank 1
answered on 20 Nov 2014, 10:15 AM
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.
0
Doroteya
Telerik team
answered on 24 Nov 2014, 02:06 PM
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.


Regards,
Doroteya
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Allen
Top achievements
Rank 1
answered on 29 Nov 2014, 01:09 AM
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
Tags
LINQ (LINQ specific questions)
Asked by
Muhammad
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Allen
Top achievements
Rank 1
Doroteya
Telerik team
Share this question
or