locked
Linq: how do i write out the value of each cell in a row RRS feed

  • Question

  • Hey all

    Im fairly new to Linq, but just wondering if its possible to write out a whole row of data from a table i am accessing.

    Currently, i am using Iqueryable<mytable> to find an entry which contains a keyword, and the writing out each columns entry (for that row) individually using 'mytableobject.ColumnName'

    I am using the 'Select' call to select the record which contains my keyword, but is it possible to write out the entry for all columns in that row?

    If that doesnt make sense, heres an example:

     

    A B C D E

    aa ; jj oo ll

    aa bb cc dd ee

    aa bb cc dd ee

     

    In the above table, i want to locate the row which has a ; in it, and print out to the console all columns in that row

     

    Thanks in advance for you help

     

    G

    Thursday, November 10, 2011 11:25 AM

Answers

  • Each cell in the row belongs to a column.

    So you can select the row into a var and print it.

    Here is an example how to do it:

               var row = from t in table
                          where t.Col3 == ";"
                          select t;
    
                Console.WriteLine("cell 1: {0}, cell 2: {1}, cell 3: {2}, cell 4: {3}", row.Col1, row.Col2, row.Col3, row.Col4);
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Bob Shen Monday, November 21, 2011 3:43 AM
    Thursday, November 10, 2011 12:12 PM
  • Yes:

                for (int i = 0; i < row.Count(); i++)
                {
                    Console.Write("Cell {0}: {1}", i, row.ElementAt(i));
                }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Bob Shen Monday, November 21, 2011 3:43 AM
    Thursday, November 10, 2011 2:28 PM

All replies

  • Each cell in the row belongs to a column.

    So you can select the row into a var and print it.

    Here is an example how to do it:

               var row = from t in table
                          where t.Col3 == ";"
                          select t;
    
                Console.WriteLine("cell 1: {0}, cell 2: {1}, cell 3: {2}, cell 4: {3}", row.Col1, row.Col2, row.Col3, row.Col4);
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Bob Shen Monday, November 21, 2011 3:43 AM
    Thursday, November 10, 2011 12:12 PM
  • Thanks for your reply, thats how im doing it at the moment.  However, lets say i have a silly table with 100 columns, and dont want to write out each column manually, is there a way to do that?

    something along the lines of 

    Console.WriteLine("my whole row of data is: "+ t.all);

    Thanks

    Thursday, November 10, 2011 12:52 PM
  • Yes:

                for (int i = 0; i < row.Count(); i++)
                {
                    Console.Write("Cell {0}: {1}", i, row.ElementAt(i));
                }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Bob Shen Monday, November 21, 2011 3:43 AM
    Thursday, November 10, 2011 2:28 PM