score:6

Accepted answer

when your getting the item from a list its an reference type, if your updating anything to it then it will automatically change the values in list. please check your self after updating...........

item whichever your getting from

userdata.where(s => s.id == idkey).tolist(); 

is an reference type.

score:0

if i'm misunderstanding you then please correct me, but i think you're saying that you essentially want to iterate through the elements of a list, and if it matches a condition then you want to alter it in some way and add it to another list.

if that's the case, then please see the code below to see how to write an anonymous method using the where clause. the where clause just wants an anonymous function or delegate which matches the following:

parameters: elementtype element, int index -- return: bool result

which allows it to either select or ignore the element based upon the boolean return. this allows us to submit a simple boolean expression, or a more complex function which has additional steps, as follows:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace stackoverflow
{
    class program
    {
        static void main(string[] args)
        {
            int idkey = 1;
            list<someclass> userdata = new list<someclass>()
            {
                new someclass(),
                new someclass(1),
                new someclass(2)
            };
            //this operation actually works by evaluating the condition provided and adding the
            //object s if the bool returned is true, but we can do other things too
            userdata.where(s => s.id == idkey).tolist();
            //we can actually write an entire method inside the where clause, like so:
            list<someclass> filteredlist = userdata.where((s) => //create a parameter for the func<someclass,bool> by using (varname)
                {
                    bool thebooleanthatactuallygetsreturnedintheaboveversion =
                        (s.id == idkey);
                    if (thebooleanthatactuallygetsreturnedintheaboveversion) s.name = "changed";
                    return thebooleanthatactuallygetsreturnedintheaboveversion;
                }
            ).tolist();

            foreach (someclass item in filteredlist)
            {
                console.writeline(item.name);
            }
        }
    }
    class someclass
    {
        public int id { get; set; }
        public string name { get; set; }
        public someclass(int id = 0, string name = "defaultname")
        {
            this.id = id;
            this.name = name;
        }
    }
}

score:1

just fetch the object using singleordefault and make related changes; you do not need to add it to the list again; you are simply changing the same instance which is an element of the list.

var temp = userdata.singleordefault(s => s.id == idkey);
// apply changes
temp.x = somevalue;

score:2

as long as userdata is reference type, the list only holds references to instances of that object. so you can change its properties without the need of remove/insert (and obviously do not need index of that object).

i also suggest you want to use single method (instead of tolist()) as long as the id is unique.

example

public void changeusername(list<userdata> users, int userid, string newname)
{
     var user = users.single(x=> x.userid == userid);
     user.name = newname;  // here you are changing the name value of userdata objects, which is still part of the list
}

score:7

you can get the index using the method userdata.findindex(s => s.id == idkey) it will return an int.


Related Query

More Query from same tag