取消LINQ更改集合中的值 [英] Uning LINQ to change values in collection

查看:65
本文介绍了取消LINQ更改集合中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我不太了解LINQ.我想做:

I think I'm not undertanding LINQ well. I want to do:

foreach (MyObjetc myObject in myObjectCollection)
{
    myObjet.MyProperty = newValue
}

只需更改集合中所有元素中某个属性的所有值即可.

Just change all values for a property in all elements of my collection.

不是那样使用LINQ吗?

Using LINQ wouldn't be this way?

myObjectCollection.Select(myObject => myObject.MyProperty = newValue)

它不起作用.属性值未更改.为什么?

It doesn't work. The property value is not changed. Why?

对不起,伙计们.当然,foreach是正确的方法.但是,就我而言,我必须在许多集合中重复foreach,并且我不想重复循环.因此,最后,我找到了一种中级"解决方案,即"foreach"方法,与"Select"类似:

Sorry, guys. Certainly, foreach is the right way. But,in my case, I must repeat the foreach in many collections, and I didn't want to repeat the loop. So, finally, I have found an 'inermediate' solution, the 'foreach' method, just similar to the 'Select':

myObjectCollection.ForEach(myObject => myObject.MyProperty = newValue)

无论如何,它并不像更简单的那样清晰:

Anyway may be it's not as clear as the more simple:

foreach (MyObjetc myObject in myObjectCollection) myObjet.MyProperty = newValue;

推荐答案

首先,这不是一个好主意.请参阅下面的内容以反对它.

First off, this is not a good idea. See below for arguments against it.

它不起作用.属性值未更改.为什么?

It doesn't work. The property value is not changed. Why?

它不起作用,因为Select()实际上直到您枚举其结果才真正遍历该集合,并且它需要一个表达式来求值.

It doesn't work because Select() doesn't actually iterate through the collection until you enumerate it's results, and it requires an expression that evaluates to a value.

如果使表达式返回一个值,并在其末尾添加可以完全评估查询的内容,例如ToList(),则它将起作用",即:

If you make your expression return a value, and add something that will fully evaluate the query, such as ToList(), to the end, then it will "work", ie:

myObjectCollection.Select(myObject => { myObject.MyProperty = newValue; return myObject;}).ToList();

话虽这么说,ToList()有一些缺点-主要是,它正在做很多不需要的额外工作(创建List<T>),这会增加大量成本.避免这种情况需要枚举集合:

That being said, ToList() has some disadvantages - mainly, it's doing a lot of extra work (to create a List<T>) that's not needed, which adds a large cost. Avoiding it would require enumerating the collection:

foreach(var obj in myObjectCollection.Select(myObject => { myObject.MyProperty = newValue; return myObject; })) 
{ }

同样,我不会推荐这个.此时,Select选项要丑陋得多,键入更多,等等.这也违反了LINQ涉及的期望-LINQ是关于查询的,这表明不应有副作用使用LINQ时,这里的全部目的是创建副作用.

Again, I wouldn't recommend this. At this point, the Select option is far uglier, more typing, etc. It's also a violation of the expectations involved with LINQ - LINQ is about querying, which suggests there shouldn't be side effects when using LINQ, and the entire purpose here is to create side effects.

但是,此时,您最好(以较少的键入)以清晰"的方式进行操作:

But then, at this point, you're better off (with less typing) doing it the "clear" way:

foreach (var obj in myObjectCollection)
{
     obj.MyProperty = newValue;
}

这是简短的,意图很明确,也很干净.

This is shorter, very clear in its intent, and very clean.

请注意,您可以添加ForEach<T>扩展方法,该方法对每个对象执行操作,但是我仍然建议避免这样做.埃里克·利珀特(Eric Lippert)写了一篇很棒的博客文章,内容值得一读:.

Note that you can add a ForEach<T> extension method which performs an action on each object, but I would still recommend avoiding that. Eric Lippert wrote a great blog post about the subject that is worth a read: "foreach" vs "ForEach".

这篇关于取消LINQ更改集合中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆