Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is it possible to create a datatable from the LINQ?
Message
From
19/08/2013 03:48:17
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01580581
Message ID:
01580745
Views:
26
>>>I can not convert row into DataRow, therefore I don't see a way to access each individual column of that resulting query. David's class solves this problem, but if I can create a simpler code, I'd love to.
>>
>>
>>I haven't tried with anonymous types, I mostly use concrete classes to select into. But through reflection you can enumerate the fields & properties in the object and then create the Data table based on these..... If the extension method works for you and creates what you need, this will probably be the simplest solution going forward...
>
>Yes, that class does work.
>
>But I found (actually, David replied in my e-mail) that I can use properties of this anonymous type.
>
>So, looks like I can simply use code like this
>
>
>foreach (var row in resultingQuery)
>                     {
>                        DataRow newRow = dtReturn.NewRow();
>
>                        newRow["resource1"] = row.resource1;
>                        newRow["fname"] = row.fname;
>
>                        dtReturn.Rows.Add(newRow);
>                     }
>
>
>Wondering if this is easier than David's class or not.

Not withstanding Gregory's suggestion for improvements this implies that you have already constructed the DataTable with appropriate column types. If the data you are pulling in has a consistent format that's fine. If not then the extension method will allow the DataTable columns to be built automatically.

I guess you should speed test both approaches ?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform