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?

3
  • LINQ to Dataset won't help you in this case. Doing a foreach loop makes more sense. Commented May 13, 2011 at 17:23
  • Is there a reason you're using DataTable instead of specific class? Commented May 13, 2011 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. Commented May 13, 2011 at 17:57

4 Answers 4

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"]));
}
Sign up to request clarification or add additional context in comments.

1 Comment

i will use ForEach. Thanks for the answer. I did think that it may be complex to do it with LINQ.
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;
    });

1 Comment

Wow. I used ForEach but your answer is very good. Exactly what i wanted. I will have to understand your query. Thanks a lot.
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.

2 Comments

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, 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.
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));

1 Comment

i am totally obsessed with LINQ. ;) Your answer is appreciated a lot.

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.