tags:

views:

31

answers:

1

I have the row collection and the Column names as list , is there way to pass all the column names using the delegate to a single row and return the values as a collection?. or is there any other best way to accompolish this?.

A: 

Are you saying you have a DataRow and you want the value in each column?

Then all you need is this:

object[] values = row.ItemArray;

If you only want the values from specific columns, I'd recommend some LINQ magic to give them to you in the form of a Dictionary<string, object>:

IEnumerable<string> columnNames = GetDesiredColumnNames();

var values = columnNames
    .ToDictionary(name => name, name => row[name]);

Of course, you could also use this same LINQ magic to get a Dictionary<string, object> for all columns (if object[] isn't descriptive enough for your liking):

IEnumerable<string> columnNames = row.Table.Columns
    .Cast<DataColumn>()
    .Select(column => column.ColumnName);

var values = columnNames
    .ToDictionary(name => name, name => row[name]);
Dan Tao
Thats right Dan,The magic indeed worked awesome :-).Thank you.
Webbies