本文介绍了如何使用linq更改对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下陈述,如果此集合的isdefault为true,则需要将每个对象的isDefault属性设置为false.

I have the following statment that if isdefault is true to this collection i need to set each object isDefault property to false.

  custHead.lstCustomziation.Where(x => x.IsDefaultSelected == true).Select(x=>{x.IsDefaultSelected=false});

lstCustomziation  is a collection.

推荐答案

LINQ用于查询.您应该使用foreach循环进行更改:

LINQ is for querying. You should use a foreach loop to make changes:

foreach (var item in custHead.lstCustomziation.Where(x => x.IsDefaultSelected))
{
    item.IsDefaultSelected = false;
}

也就是说,如果其他项目的IsDefaultSelectedfalse,则只是无条件设置它可能会更简单:

That said, if IsDefaultSelected is false for the other items anyway, it may be simpler just to unconditionally set it:

foreach (var item in custHead.lstCustomziation)
{
    item.IsDefaultSelected = false;
}

这篇关于如何使用linq更改对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:28