You can populate data using a Lambda expression (C # 3.0)

I have a datatable. I inhabit some values. eg.

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 with this program and gives the expected result.

However, recently I have been studying Lambda and have done some basic knowledge.

With this I tried to do the same as in

Enumerable.Range(0,9).Select(i = > 
{

    dt.Rows.Add(i,"String"  + i.toString(),DateTime.Now,(i%2 == 0)?true:false);
});

But I did not succeed.

Is my approach correct (yes, I know that I get a compile-time error, because there is not enough knowledge on this issue yet)?

Can we achieve this, by the way, I make this a big doubt (as I don’t know ... just give a shot).

If so, can someone help me in this regard.

I am using C # 3.0 and dotnet framework 3.5

thank

+3
source share
2 answers

. { } "= >".

Enumerable.Range(0, 9).Select(i => dt.Rows.Add(i, 
    "String" + i.ToString(), DateTime.Now, (i%2 == 0) ? true : false));
+2

Select . execute, . - dt.Rows.Add void, Action, Func Select.

Select,

ToList<T>().ForEach<T>(Action<T>)

:

Enumerable.Range(0,9)
  .ToList()
  .ForEach(i =>  
  {
    dt.Rows.Add(i,"String"  + i.ToString(), DateTime.Now, (i%2 == 0) );
  });
+2

Source: https://habr.com/ru/post/1743732/


All Articles