0

I have a datatable and i want to print the content of the table to console.

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("FName");
dt.Columns.Add("LName");

DataRow dr;
dr = dt.NewRow();
dr["FName"] = "FName1";
dr["LName"] = "LName1";
dt.Rows.Add(dr);


dr = dt.NewRow();
dr["FName"] = "FName2";
dr["LName"] = "LName2";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["FName"] = "FName3";
dr["LName"] = "LName3";
dt.Rows.Add(dr);

I want output on console like this:

Row 1: FNAme = Fname1, LName = LName1
Row 2: FNAme = Fname2, LName = LName2
Row 2: FNAme = Fname3, LName = LName3

How can i do this using LINQ?

Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • LINQ to Dataset won't help you in this case. Doing a foreach loop makes more sense. – Thomas Li May 13 '11 at 17:23
  • Is there a reason you're using `DataTable` instead of specific class? – svick May 13 '11 at 17:34
  • I am using TableParaeter in the SQL query. Just for the debugging purpose, i wanted to print the values being passed to the SP. – Asdfg May 13 '11 at 17:57

4 Answers4

5

This has nothing to do with LINQ, you will need to iterate over the items.

StringBuilder sb = new StringBuilder();
foreach (DataRow dr in DataTable.Rows)
{
    sb.Append(String.Format("Row {0}: FNAme = {1}, LName = {2}", dt.Rows.IndexOf(row) + 1, dr["FName"], dr["LName"]));
}
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
  • i will use ForEach. Thanks for the answer. I did think that it may be complex to do it with LINQ. – Asdfg May 13 '11 at 18:19
4

This isn't really the purpose of LINQ but if you really want...

dt.Rows.Cast<DataRow>()
  .Select((DataRow dr, int i) =>
    {
      return String.Format("Row {0}: FNAme = {1}, LName = {2}", i, dr["FName"], dr["LName"]);
    })
  .All((line) =>
    {
      Console.WriteLine(line);
      return true;
    });
Krypes
  • 561
  • 2
  • 10
  • Wow. I used ForEach but your answer is very good. Exactly what i wanted. I will have to understand your query. Thanks a lot. – Asdfg May 14 '11 at 14:41
3

So you could use LINQ and write something like this

DataTable.Rows.ToList().ForEach(r => Console.WriteLine(String.Format("Row {0}: FNAme = {1}, LName = {2}", dr.Rows.IndexOf(r) + 1, r.Item["FName"], r.Item["LName"])

But for what you're doing a foreach loop makes more sense

//Borrowing Dustin Lanes foreach here.

StringBuilder sb = new StringBuilder();
foreach (DataRow dr in DataTable.Rows)
{
    sb.Append(String.Format("Row {0}: FNAme = {1}, LName = {2}", dt.Rows.IndexOf(row) + 1, dr["FName"], dr["LName"]));
}

The reason that I say that a foreach loop makes more sense is that it's clearer what you are doing. The implementation is simpler.

LINQ is great, but I try to only use it when it provides a clearer representation of what I am doing. It's honestly becoming the defacto answer for any thing to do with any code dealing with collections and sometimes it isn't the answer.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • When i was using ForEach, people used to say "Use LINQ" and now when i am using LINQ for almost everything, i am told to use ForEach. I guess it entirely depends on the cleanliness of the code. – Asdfg May 13 '11 at 18:22
  • 1
    @ASdfg, it's entirely situational, you wouldn't use a chainsaw to pull weeds, and you wouldn't use a knife to cut down a tree. – msarchet May 13 '11 at 18:24
1

A more generic solution (also using some LINQ, since you seem to have the hots for it ;-))

DataTable dt = ...;

string formatStr = String.Join(", ", Enumerable.Range(0, dt.Columns.Count)
    .Select(c => dt.Columns[c].ColumnName + " = {" + c + "}").ToArray());

for (int i = 0; i < dt.Rows.Count; i++)
    Console.WriteLine("Row " + i + ": " + String.Format(formatStr, dt.Rows[i].ItemArray));
SirViver
  • 2,411
  • 15
  • 14