how to remove from list using Lambda syntax

c#, lambda, list

Solution

names.RemoveAll(x => x.UserName == name);

Note here that all the lambda syntax does is provide a `Predicate<T>`; lambda syntax is entirely unrelated to what it ends up doing with the lambda.

Or for a single match (see comments):

var found = names.Find(x => x.UserName == name);
if(found != null) names.Remove(found);

or:

var index = names.FindIndex(x => x.UserName == name);
if(index >= 0) names.RemoveAt(index);

Problem

Given: ``` 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); } ``` What's the Lambda syntax to execute the removal? And how can I get indication of "success" if the function did remove or not?

Original source