Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is it possible to create a datatable from the LINQ?
Message
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01580581
Message ID:
01580772
Vues:
30
>>>>>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 ?
>
>When I run the test, it shows 2 seconds for the whole method either way. I'll add measuring time to see what's quicker although I think this way is going to be slightly quicker.

This probably isn't something that will be executed very often. If both methods run in around 2 seconds it isn't worth your time to investigate which one runs a few milliseconds faster. IMO, of course, and not being critical.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform