Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there is a C# function which is similar to .Map() in JavaScript? [duplicate]

Hi I have some background in Javascript, and I used .filter() and .map() a lot, recently I have a c# project, I am new to c# just wondering is there a C# function which is similar to .Map()in JavaScript? I know that .where() is very similar to .filter().

btw .map() is like

    const newArray = array1.map(
          el => {
                if(el.id===1){
                el.name='foo';
                return el;
                 }
               return el;});

and it returns a new array. .FroEach() does not return a new array.

My account got blocked by some down votes questions, the funny thing is I have to re-edit them, even though I already have the accepted answer.I do not understand what's the point to do this.I am so frustrated by this stackoverflow system.

Now, I basically can do nothing but keep editing my questions, and they have all been answered. This is ridiculous !!!

like image 535
Andy Song Avatar asked Nov 16 '18 21:11

Andy Song


1 Answers

You can Use .Select():

array.Select(el =>
    {
        if(el.id == 1)
        {
            el.name="foo";
            return el;
        }
        return el;
    }
);
like image 51
Ashkan Mobayen Khiabani Avatar answered Sep 17 '22 13:09

Ashkan Mobayen Khiabani