“List.Remove” in C# does not remove item?

后端 未结 6 662
面向向阳花
面向向阳花 2020-12-11 03:04

Hello how can i remove item from generic list here is my code im trying to do it right but i dont know where i make mistake;/

Users us_end = new Users();
for         


        
6条回答
  •  半阙折子戏
    2020-12-11 03:13

    You are creating a new Users object - this is not the same as any object already in Application["Users_On"] (it will have a different reference), so it will not be removed.

    This assumes that Equals and/or IEquatable were not overridden/implemented in Users.

    List us = ((List)Application["Users_On"]);
    Users us_end = us.Where(u => u.Id == (int)Session["Current_Id"]).FirstOrDefault();
    us.Remove(us_end);
    Application["Users_On"] = us;
    

    By the way - your variable naming is not very good - go for more descriptive names.

提交回复
热议问题