鉴于:
List<Name> names = new List<Name>(); //list full of names public void RemoveName(string name) { List<Name> n = names.Where(x => x.UserName == name);; names.Remove(n); }
执行删除的Lambda语法是什么?
解决方法
names.RemoveAll(x => x.UserName == name);
请注意,所有的lambda语法都提供了一个谓词< T>; lambda语法与使用lambda完成的任务完全无关.
或者参加一场比赛(见评论):
var found = names.Find(x => x.UserName == name); if(found != null) names.Remove(found);
要么:
var index = names.FindIndex(x => x.UserName == name); if(index >= 0) names.RemoveAt(index);