0
votes

I'd like to use LINQ to take a datarow, and parse out the datacolumn names with their values.

So if I had a dataRow with the following columns and values:

DataColumn column1 with value '1'
DataColumn column2 with value 'ABC'

I'd like to have a string returned as "column1 = '1' and column2 = 'ABC'"

**** code should not care about the column names, nor the number of columns in the table.****

Purpose being, to filter a dataTable like:

var newRows = myTable.Select ("column1 = '1' and column2 = 'ABC'");

I can parse out the columns of the table like this:

string[] columnName = myTable.Columns.Cast<DataColumn>().Select(cn => cn.ColumnName).ToArray();

But I need to also extract values from a target row. It feels like this might be a start:

{
  string[] columnNames = existingTable.Columns.Cast<DataColumn>().Select(cn => cn.ColumnName).ToArray();
  foreach (DataRow oldRow in existingTable.Rows)
  {
    var criteria = string.Join("and", columnNames, oldRow.ItemArray);
  }
}
3

3 Answers

1
votes

I think you are trying to get the column names and rows without actually referencing them. Is this what you're trying to do:

var table = new DataTable();

var column = new DataColumn {ColumnName = "column1"};
table.Columns.Add(column);

column = new DataColumn {ColumnName = "column2"};
table.Columns.Add(column);

var row = table.NewRow();
row["column1"] = "1";
row["column2"] = "ABC";
table.Rows.Add(row);

string output = "";
foreach (DataRow r in table.Rows)
{
    output = table.Columns.Cast<DataColumn>()
                  .Aggregate(output, (current, c) => current + 
                      string.Format("{0}='{1}' ", c.ColumnName, (string) r[c]));
    output = output + Environment.NewLine;
}

// output should now contain "column1='1' column2='ABC'"

You can create extension methods out of this too, which operate both on a DataTable (for all rows) or a DataRow (for a single row):

public static class Extensions
{
    public static string ToText(this DataRow dr)
    {
        string output = "";
        output = dr.Table.Columns.Cast<DataColumn>()
                   .Aggregate(output, (current, c) => current +
                       string.Format("{0}='{1}'", c.ColumnName, (string)dr[c]));

        return output;
    }

    public static string ToText(this DataTable table)
    {
        return table.Rows.Cast<DataRow>()
                    .Aggregate("", (current, dr) => current + dr.ToText() + Environment.NewLine);
    }
}
0
votes

I would highly recommend mapping to a class, and then adding another property which combines both of them!

0
votes

See if this gets you in the right direction

EDIT Added solution using reflection since that is more in line with what you are wanting to accomplish

void Main()
{
    List<MyClass> myColl = new List<MyClass>() { new MyClass() { myFirstProp = "1", mySecondProp = "ABC" } };

    foreach (MyClass r in myColl)
    {
        List<string> rPropsAsStrings = new List<string>();

        foreach (PropertyInfo propertyInfo in r.GetType().GetProperties())
        {
            rPropsAsStrings.Add(String.Format("{0} = {1}", propertyInfo.Name, propertyInfo.GetValue(r, null)));
        }

        Console.WriteLine(String.Join(" and ", rPropsAsStrings.ToArray()));
    }


}

public class MyClass
{
    public string myFirstProp { get; set; }
    public string mySecondProp { get; set; }
}

The below uses Linq with strong typed properties

System.Data.DataTable table = new DataTable("ParentTable");
DataColumn column;
DataRow row;

column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "column1";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "column2";
table.Columns.Add(column);

row = table.NewRow();
row["column1"] = "1";
row["column2"] = "ABC";
table.Rows.Add(row);

var results = from myRow in table.AsEnumerable()
                select String.Format("column1 = {0}, column2 = {1}", myRow[0], myRow[1]);

foreach (string r in results)
{
    Console.WriteLine(r);
}