开发者

Multiple Selection in ForEach Extension Method

Is it possible to select multiple entiies in ForEach Extension Method?

(i.e开发者_运维问答)

partial code is given

DataTableA.AsEnumerable().ToList().
    ForEach(x=>
    {
        x.SetField<string>("Name","Jon Skeet"),
        x.SetField<string>("msg","welcome")
    });

when i apply multiple selection in ForEach

 x=>
{
    x.SetField<string>("Name","Jon Skeet"),
    x.SetField<string>("msg","welcome")
}

I am unable to complete the statement.Help Please.


You need to make the lambda compile to valid C#:

DataTableA.AsEnumerable().ToList().ForEach(
     x=>  
     {
         x.SetField<string>("Name","Jon Skeet");
         x.SetField<string>("msg","welcome");
      });

That being said, I'd caution against this. Using ForEach is purposely causing side-effects, and it's really not more concise than the foreach statement in the language itself. Personally, I'd just write:

foreach(var element in DataTableA.AsEnumerable())
{
    element.SetField<string>("Name","Jon Skeet");
    element.SetField<string>("msg","welcome");
}

This, in my opinion, is much more clear than the first statement (and shorter, and more efficient, since it doesn't force a full enumeration into the list, and a second enumeration of the list elements).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜