且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用LINQ查询和更新集合中的属性

更新时间:2023-02-13 11:28:19

您可以更改顺序,然后使用ForEach运算符:

You can change the order, then use the ForEach operator:

tickets
   .Where(x => x.Name == "Beach")
   .ToList()
   .ForEach(x => { x.Selected = true; });

注意:

  • 由于IEnumerable在Linq中不支持ForEach,因此需要ToList()-请参见 LINQ相当于IEnumerable< T>
  • 的foreach
  • 出于可读性考虑,***将其分离为linq查询,然后再使用更常规的foreach(x in list) C#循环
  • 如果这是linq-to-sql,则需要调用SubmitChanges()才能保留更改.
  • that the ToList() is needed because IEnumerable doesn't support ForEach in Linq - see LINQ equivalent of foreach for IEnumerable<T>
  • that for readability it might be better to separate this out into a linq query and then a more conventional foreach(x in list) C# loop
  • if this is linq-to-sql, then you'll need to call SubmitChanges() in order to persist your changes.