tags:

views:

46

answers:

3

I'd like to utilize Select & Insert statements on a DataTable in VB.NET or C#.

Example:

Dim Results as DataTable 
Results = Select * from SourceDataTable where PlayerID < 10

Is anything similar to this possible?

Thanks

+5  A: 

If you're using .NET 3.5 then you can use LINQ (I don't know the VB.NET syntax, so here's C#):

var results = from row in SourceDataTable.AsEnumerable()
              where row.Field<int>("PlayerID") < 10
              select row;

It's not exactly the same, but certainly pretty close to "natural" SQL syntax. LINQ also has the advantage that it works on any collection type (and even directly against the database, using LINQ-to-SQL), not just DataTables.

Dean Harding
What would 'row' be in this code?
hamlin11
@hamlin11: `row` is a variable to hold the 'current' row as it goes through the collection. [Field](http://msdn.microsoft.com/en-us/library/bb360891.aspx) is an extension method on the `DataRow` type.
Dean Harding
+1  A: 

For select use

DataTable dt ;
dt = dt.Select("PlayerID < 10");

in C#

anishmarokey
+2  A: 

DataTables have a select method which return an array of DataRow

the filterExpression and sort paramaters take sql;

result = SourceDataTable.Select(" PlayerID < 10 ")
aaron