-1

I have a DataTable which contains column names and rows. Now as per my requirement I have to get only row value without giving the column names as I have done now.Here is the code.

data = employees.AsEnumerable().Select(row=> new List<string>
{
   row.Field<string>("EmployeeName"),
   row.Field<string>("Company")
})

In the above code employees is DataTable. How to do it.I have to get the row values into data variable as shown in code.

Update

data = employees.AsEnumerable().Select(row=> new List<string>
{
 foreach(DataRow row in employees.Rows)
    {
        foreach(DataColumn col in employees.Columns)
         data.Add(row[col.Ordinal].ToString());
     }
})
Lara
  • 2,821
  • 7
  • 39
  • 72

2 Answers2

1

You could simply use this syntax

data = employees.AsEnumerable().Select(row=> new List<string>
{ 
   row[0].ToString(),
   row[1].ToString()

});

Where 0 is the index of the column EmployeeName and 1 is the index of the column Company (if your query is something like SELECT EmployeeName, Company from ....)

But, in my opinion, this is really a step backwards. Using column names preserves your code from the order in which the columns are loaded from the database table.

EDIT If you want to loop over every row and for every row on every column you could use this code (at this point there is no much sense in using IEnumerable extensions)

With foreach:

foreach(DataRow row in employees.Rows)
   foreach(DataColumn col in employees.Columns)
       data.Add(row[col.ColumnName].ToString());
   // or 
   // data.Add(row[col.Ordinal].ToString());

With standard loop

for(int r = 0; r < employees.Rows.Count; r++)
   for(int c = 0; c < employees.Columns.Count; c++)
       data.Add(employees.Rows[r][c].ToString());
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Is there any way by which I can iterate to all rows without hardcoding it like `[0]`, `[1]` etc – Lara Aug 02 '16 at 13:28
  • you can use employees.Columns.Count for a loop. – Tom Söhne Aug 02 '16 at 13:30
  • Are you talking about columns not rows right? – Steve Aug 02 '16 at 13:31
  • Yes. using `employees.Columns.Count ` I can get total number of columns.Could you pls give me syntax how I can add that into my code. I am not able to add `foreach` into it – Lara Aug 02 '16 at 13:32
  • Updated the post after applying the shared code by you but getting syntax errors. Could you pls guide me on this – Lara Aug 02 '16 at 13:39
  • Do not use the IEnumerable extensions with my example. There is no much sense and probably is slower. Just declare the list before entering the loop with _Listdata = new List();_ – Steve Aug 02 '16 at 13:48
  • Hi @Steve Just one more help I needed. Currently I am getting data like `[data":["John Doe","Fresno","Billy","Fresno","Tom","Kern","King Smith","Kings"]}]`. How to get it like `"data":[["John Doe","Fresno"],["Billy","Fresno"],["Tom","Kern"],["King Smith","Kings"]` ` – Lara Aug 03 '16 at 09:31
  • Sorry a little busy at the moment, Why don't you post a new question highlighting your requirements? In this way more people will be able to help you because here very few people will notice the problem explained in your comment here. – Steve Aug 03 '16 at 09:48
0

maybe you can use that?

var dt = new DataTable();

dt.AsEnumerable().Select(row => new List<string>
{
    row.Field<string>(0),
    row.Field<string>(1)
});
Tom Söhne
  • 512
  • 2
  • 6