0

I want to convert the code below to a TableRow[] array using LINQ and add it to a main Table class.

foreach (DataRow dr in dt.Rows)
{
    TableRow tr = new TableRow();
    tr.Attributes.Add("onmouseover", "setMouseOverColor(this);");
    tr.Attributes.Add("onmouseout", "setMouseOutColor(this);");
    tr.Attributes.Add("onclick", "selectRow(this);");
    TableCell td = new TableCell();
    td.Text = dr["Test"].ToString();
    tr.Cells.Add(td);
    table.Rows.Add(tr);
}

Is there a way in LINQ to achieve this without going through a foreach loop?

I was able to write a TableCell[] array class as:

TableCell[] tdarr = dt.AsEnumerable().Select(p => new TableCell { Text = p.Field<string>("Test") }).ToArray();
3
  • @Learner, I don't need to convert to DataTable. I already have DataTable which i want to loop through each record to create html Table structure. In that DataTable I've one column. I want to create Table cell with DataTable column value and then add that Table cell to Table row and then to Html Table class. Hope its clear for you now.
    – Syed
    Commented Jun 11, 2013 at 15:06
  • Sorry, misread your question. Deleted my comment.
    – Learner
    Commented Jun 11, 2013 at 15:11
  • 1
    I guess even if you use LINQ, you will still have to use ForEach. If I am not wrong, what you need is to map your datatable directly to HTML table not sure if there is any sort of object to HTML table mapper available.
    – Learner
    Commented Jun 11, 2013 at 15:14

0

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.