C# 查找多个元素并根据结果设置属性

C# 查找多个元素并根据结果设置属性,c#,linq,set,C#,Linq,Set,他 我有一个列表\u用户,我想找到用户名以“toto”开头的所有用户,并为所有用户设置一个属性,bool excepted=true 我实际上有这样的代码: _users.FindAll(z => z.userName.StartWith("toto") == true && location == "London") 我想达到这样的目标: _users.FindAll(z => z.userName.StartWith("toto") == true &&

我有一个
列表\u用户
,我想找到用户名以“toto”开头的所有用户,并为所有用户设置一个属性,bool excepted=true

我实际上有这样的代码:

_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London")
我想达到这样的目标:

_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London").Cast<MyObject>().excepted = true;
\u users.FindAll(z=>z.userName.StartWith(“toto”)==true和&location==London”).Cast().excepted=true;
强制转换以了解我当前使用的对象类型,然后设置我的属性。。。。这段代码显然根本不起作用,但我不知道我该怎么做,如果可能的话:(


谢谢

你给了我高潮!啊哈,太谢谢你了!:D我需要更多的实践来回答这些问题:)这些结果之间的最佳优化是什么?,我发现了一个关于优化的很酷的话题:)答案是有效的,但它无论如何都不会优化代码。
ToList
会创建一个
List
,因此与
FindAll
非常相似。要真正优化它,请使用foreach loop over
\u用户。其中
不使用
ToList
(尽管这不会产生任何重大影响)
_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London")
      .ForEach(x=>x.excepted =true)
_users.Where(x => x.userName.Contains("toto")).ToList().ForEach(y => y.excepted = true);