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:7

You can get the index using the method UserData.FindIndex(s => s.ID == IDKey) It will return an int.

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: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;
        }
    }
}

Related Articles