0

EDIT: I'm giving the credit to Nick as his answer pointed me in the right direction. In his answer he had set all DataColumns to be retrieved as String, but not all columns in the DataTable were strings. Also Select() was appropriate to get individual rows whereas SelectMany() joined all column values for multiple rows into a single array, or one big row if you will. Here is the solution:

using (dataTable)
using (var sw = new StreamWriter(resultPath, false))
{
    DataColumn[]
          exclCols = { dataTable.Columns["registryvalue"] }, //excluded
          inclCols = dataTable.Columns //included columns
                              .Cast<DataColumn>()
                              .Except(exclCols)
                              .ToArray();

    sw.WriteLine(String.Join( //header line
                 ",", inclCols.Select(column => column.ColumnName).ToArray()));

    foreach (var row in dataTable.AsEnumerable().Select(
            dataRow => inclCols.Select(column => dataRow.Field<Object>(column))))
    {
        sw.WriteLine(String.Join(
                     ",", row.Select(r => (r == null) ? 
                     "" : r.ToString()).ToArray()));
    }
}

I'm using foreach to loop through the rows of a DataTable. There are only certain columns' values that I want from the DataRow and I want to convert this subset of values into a string array (we'll call it myStringArray). The end result is:

StreamWriter.WriteLine(String.Join(",", myStringArray)) 

I can accomplish writing the line without using LINQ, but then that doesn't help me better understand LINQ. Getting the subset of column names I want into an array is easy, but I just don't know how to use that array to get only those columns' data from the DataRow.

3
  • I re-read your question after posting my answer below... Do you need just one column's data or do you need to combine strings from multiple columns? Commented Feb 15, 2012 at 23:58
  • Yeah but I think you will end up with an Array of String[]. Because you are going to get a String[] for each row in your DataTable. Maybe you should clarify this in your question. Commented Feb 16, 2012 at 0:00
  • I'm looking to get a subset of all the objects that make up a DataRow and then convert those objects into a single string array. Commented Feb 16, 2012 at 14:07

2 Answers 2

2

EDIT 2:

Here's how to do it with LINQ

var columnNames = new [] { "Column1", "Column2", "Column3" };
var strings = dataTable.AsEnumerable().SelectMany(dataRow => columnNames.Select(columnName => dataRow.Field<String>(columnName)));

EDIT:

I'm not sure how to do that with LINQ. It is simple to express using two foreach loops though

String [] columnNames = new String [] { "Column1", "Column2", "Column3" };
var myStrings = new List<String>();
foreach (DataRow dataRow in dataTable.Rows)
    foreach (String columnName in columnNames)
        myStrings.Add(dataRow.Field<String>(columnName));

ORIGINAL:

You'll need DataTable.AsEnumerable() extension method to use LINQ against your DataTable

Something like this will do the job...

var myStrings = from dataRow in dataTable.AsEnumerable()
                select dataRow.Field<String>("TheColumnName");
Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping to avoid explicitly naming the columns I wanted, but rather use the array that contains the column names I want included.
Your Edit2 got me going in the right direction. See my edit to the original post. Thanks!
0

There are probably a variety of answers to this, but I will take a stab at one that might be illuminating. My syntax is rusty, so please forgive errors, the theory should be good.

foreach (var row in from it in myDataTable select new { Col1 = it.Col1, Col2 = it.Col2 })
    //Do something with the subset of rows that is interesting.

This will get you an anonymous type that has the fields Col1 and Col2 in it. You can select any subset you like.

foreach (var myArray in from it in myDataTable select new string[] { it.Col1, it.Col2 })
    //Do something with the string array

I have not tried this as a string array has never been a target, but it you experiment you can probably make it work.

LINQ offers a variety of syntax choices, this one assumes you are in c# using declarative syntax.

1 Comment

Unfortunately this returns only an array of basically the same information I already have in my included columns array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.