how to remove from list using Lambda syntax

前端 未结 2 1155
刺人心
刺人心 2021-02-12 10:11

Given:

List names = new List(); //list full of names

public void RemoveName(string name) {
   List n = names.Where(x => x         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-12 10:14

    var n = names.SingleOrDefault(x => x.UserName == name)
    if(n != null && n.UserName.length > 0)
    {
     names.Remove(n);
    }
    

    OR

    var n= names.Where(x => x.UserName == name).First();
    names.Remove(n)
    

提交回复
热议问题