Reading column header and column values of a data table using LAMBDA(C#3.0)

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-05-04T03:54:13Z Indexed on 2010/05/04 3:58 UTC
Read the original article Hit count: 263

Filed under:

Consider the folowing where I am reading the data table values and writing to a text file

using (StreamWriter sw = new StreamWriter(@"C:\testwrite.txt",true))
            {
                DataPreparation().AsEnumerable().ToList().ForEach(i =>
                {
                    string col1 = i[0].ToString();
                    string col2 = i[1].ToString();
                    string col3 = i[2].ToString();
                    string col4 = i[3].ToString();
                    sw.WriteLine(
                        col1 + "\t"
                        + col2 + "\t" +
                        col3 + "\t" +
                        col4 + Environment.NewLine
                        );
                });
            }

The data preparation function is as under

private static DataTable DataPreparation()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Col1", typeof(string));
            dt.Columns.Add("Col2", typeof(int));
            dt.Columns.Add("Col3", typeof(DateTime));
            dt.Columns.Add("Col4", typeof(bool));

            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add("String" + i.ToString(), i, DateTime.Now.Date, (i % 2 == 0) ? true : false);
            }
            return dt;
        }

It is working fine.

Now in the above described program, it is known to me the Number of columns and the column headers.

How to achieve the same in case when the column headers and number of columns are not known at compile time using the lambda expression?

I have already done that which is as under

public static void WriteToTxt(string directory, string FileName, DataTable outData, string delimiter)
        {
            FileStream fs = null;
            StreamWriter streamWriter = null;
            using (fs = new FileStream(directory + "\\" + FileName + ".txt", FileMode.Append,
                                            FileAccess.Write))
            {
                try
                {

                    streamWriter = new StreamWriter(fs);
                    streamWriter.BaseStream.Seek(0, SeekOrigin.End);                   
                    streamWriter.WriteLine();
                    DataTableReader datatableReader = outData.CreateDataReader();
                    for (int header = 0; header < datatableReader.FieldCount; header++)
                    {
                        streamWriter.Write(outData.Columns[header].ToString() + delimiter);
                    }
                    streamWriter.WriteLine();
                    int row = 0;
                    while (datatableReader.Read())
                    {
                        for (int field = 0; field < datatableReader.FieldCount; field++)
                        {
                            streamWriter.Write(outData.Rows[row][field].ToString() + delimiter);
                        }
                        streamWriter.WriteLine();
                        row++;
                    }
                }               
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

I am using C#3.0 and framework 3.5

Thanks in advance

© Stack Overflow or respective owner

Related posts about c#3.0