Is is possible to populate a datatable using a Lambda expression(C#3.0)
- by deepak.kumar.goyal
I have a datatable. I am populating some values into that.
e.g.
DataTable dt =new DataTable();
dt.Columns.Add("Col1",typeof(int));
dt.Columns.Add("Col2",typeof(string));
dt.Columns.Add("Col3",typeof(DateTime));
dt.Columns.Add("Col4",typeof(bool));
for(int i=0;i< 10;i++)
dt.Rows.Add(i,"String" + i.toString(),DateTime.Now,(i%2 == 0)?true:false);
There is nothing wrong in this program and gives me the expected output.
However, recently , I am learning Lambda and has done some basic knowledge.
With that I was trying to do the same thing as under
Enumerable.Range(0,9).Select(i = >
{
dt.Rows.Add(i,"String" + i.toString(),DateTime.Now,(i%2 == 0)?true:false);
});
But I am unsuccessful.
Is my approach correct(Yes I know that I am getting compile time error; since not enough knowledge on the subject so far)?
Can we achieve this by the way I am doing is a big doubt(as I donot know.. just giving a shot).
If so , can some one please help me in this regard.
I am using C#3.0 and dotnet framework 3.5
Thanks