鉴于:
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 语法所做的只是提供一个Predicate<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);
var n = names.SingleOrDefault(x => x.UserName == name)
if(n != null && n.UserName.length > 0)
{
names.Remove(n);
}
或者
var n= names.Where(x => x.UserName == name).First();
names.Remove(n)