Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Transformations on returned data set
Message
De
09/05/2013 16:34:51
 
 
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
Divers
Thread ID:
01573095
Message ID:
01573291
Vues:
38
>>>Hi everybody,
>>>
>>>In VFP I have code that first gets data from SQL Server into a cursor and then does extra select from that cursor into another cursor applying some extra formatting.
>>>
>>>I am wondering what is the best way to simulate this in C#? I understand I can get dataset (or reader) and loop through every row and generate a new dataset.
>>>
>>>Is there another way using LINQ, for example? If yes, can you show a sample, please?
>>>
>>>Thanks in advance.
>>
>>I don't want to be "that" guy, but... is there a reason you're using datasets? With custom classes you have much more flexibility.
>
>This is what I started to write
>
>
>var results = from r in ds.Tables[0].AsEnumerable()
>                                select new {resource1 = r.Field<String>("resource1") };
>
>
>My problem is that I will need to pass that result as datatable to another method.
>
>I am not sure how can I do that - transform this LINQ result as datatable?

Try this (untested):
var newTable = new DataTable();
newTable.Columns.Add("resource1");
var results = (from r in ds.Tables[0].AsEnumerable()
			  select newTable.Rows.Add(r.Field<String>("resource1")))
			  .ToList(); // Force immediate execution
// newTable has the results as a DataTable, results has it as a List< DataRow>
or
var newTable = new DataTable();
newTable.Columns.Add("resource1");
var results = ds.Tables[0].AsEnumerable()
				.Select(r =>
					{
						var newRow = newTable.NewRow();
						newRow["resource1"] = r["resource1"]; // Or loop through ds.Tables[0].Columns
							return newRow;
					})
				.CopyToDataTable();
// results is the new DataTable, newTable is only used for the structure
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform