<bdo id='xaPYm'></bdo><ul id='xaPYm'></ul>
    1. <small id='xaPYm'></small><noframes id='xaPYm'>

      <legend id='xaPYm'><style id='xaPYm'><dir id='xaPYm'><q id='xaPYm'></q></dir></style></legend>
      <tfoot id='xaPYm'></tfoot>

      1. <i id='xaPYm'><tr id='xaPYm'><dt id='xaPYm'><q id='xaPYm'><span id='xaPYm'><b id='xaPYm'><form id='xaPYm'><ins id='xaPYm'></ins><ul id='xaPYm'></ul><sub id='xaPYm'></sub></form><legend id='xaPYm'></legend><bdo id='xaPYm'><pre id='xaPYm'><center id='xaPYm'></center></pre></bdo></b><th id='xaPYm'></th></span></q></dt></tr></i><div id='xaPYm'><tfoot id='xaPYm'></tfoot><dl id='xaPYm'><fieldset id='xaPYm'></fieldset></dl></div>

        如何在 C# 中使用 LINQ 更新对象列表的单个项目

        How to update a single item of liist of objects using LINQ in C#(如何在 C# 中使用 LINQ 更新对象列表的单个项目)
      2. <small id='17owc'></small><noframes id='17owc'>

            • <legend id='17owc'><style id='17owc'><dir id='17owc'><q id='17owc'></q></dir></style></legend>
                <bdo id='17owc'></bdo><ul id='17owc'></ul>
                <tfoot id='17owc'></tfoot>
                  <tbody id='17owc'></tbody>

                <i id='17owc'><tr id='17owc'><dt id='17owc'><q id='17owc'><span id='17owc'><b id='17owc'><form id='17owc'><ins id='17owc'></ins><ul id='17owc'></ul><sub id='17owc'></sub></form><legend id='17owc'></legend><bdo id='17owc'><pre id='17owc'><center id='17owc'></center></pre></bdo></b><th id='17owc'></th></span></q></dt></tr></i><div id='17owc'><tfoot id='17owc'></tfoot><dl id='17owc'><fieldset id='17owc'></fieldset></dl></div>

                  本文介绍了如何在 C# 中使用 LINQ 更新对象列表的单个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想更新具有 Text=ALL" 属性的列表的 Value.

                  I want like to update the Value of the list which has property Text="ALL".

                  public class Season
                      {
                        public string Text {get;set;}
                        public string Value {get;set;}
                        public bool ValueSelected {get;set;}
                      }
                  

                  推荐答案

                  LINQ 中的Q"代表查询".LINQ 并不意味着更新对象.

                  The 'Q' in LINQ stands for "Query". LINQ is not meant to update objects.

                  您可以使用 LINQ 找到要更新的对象,然后传统地"更新它.

                  You can use LINQ to find the object you want to update and then update it "traditionally".

                  var toUpdate = _seasons.Single(x => x.Text == "ALL");
                  
                  toUpdate.ValueSelected = true;
                  

                  此代码假定恰好有 一个 条目带有 Text == "ALL".如果没有或有多个,此代码将抛出异常.

                  This code assumes that there is exactly one entry with Text == "ALL". This code will throw an exception if there is none or if there are multiple.

                  如果没有或没有,请使用SingleOrDefault:

                  If there is either none or one, use SingleOrDefault:

                  var toUpdate = _seasons.SingleOrDefault(x => x.Text == "ALL");
                  
                  if(toUpdate != null)
                      toUpdate.ValueSelected = true;
                  

                  如果可能有多个,请使用 Where:

                  If it's possible that there are multiple, use Where:

                  var toUpdate = _seasons.Where(x => x.Text == "ALL");
                  
                  foreach(var item in toUpdate)
                      item.ValueSelected = true;
                  

                  这篇关于如何在 C# 中使用 LINQ 更新对象列表的单个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                    <legend id='hOeHZ'><style id='hOeHZ'><dir id='hOeHZ'><q id='hOeHZ'></q></dir></style></legend>

                        <bdo id='hOeHZ'></bdo><ul id='hOeHZ'></ul>
                        <tfoot id='hOeHZ'></tfoot>
                          <tbody id='hOeHZ'></tbody>
                        <i id='hOeHZ'><tr id='hOeHZ'><dt id='hOeHZ'><q id='hOeHZ'><span id='hOeHZ'><b id='hOeHZ'><form id='hOeHZ'><ins id='hOeHZ'></ins><ul id='hOeHZ'></ul><sub id='hOeHZ'></sub></form><legend id='hOeHZ'></legend><bdo id='hOeHZ'><pre id='hOeHZ'><center id='hOeHZ'></center></pre></bdo></b><th id='hOeHZ'></th></span></q></dt></tr></i><div id='hOeHZ'><tfoot id='hOeHZ'></tfoot><dl id='hOeHZ'><fieldset id='hOeHZ'></fieldset></dl></div>

                        • <small id='hOeHZ'></small><noframes id='hOeHZ'>