C#:更新列表< of>中的项目值. [英] C#: Update Item value in List<of T>
问题描述
我正在寻找一种方法来更新列表中的元素,而无需自己枚举.
I'm looking for a way to update an Element in a List without enumerating it on my own.
我得到了MyProjects类,其中包含一个名为Projects的列表.
I got the Class MyProjects which hold a List named Projects.
我想找到MyProjects.Projects-Class,其中Class1(名称)的成员属性等于值开销".
I want to find the MyProjects.Projects-Class, where a member property of Class1 (Name) equals the Value "Overhead".
有效方法:
foreach (Project prj in MyProjects.Projects) {
if (prj.Name == "Overhead")
prj.IsActive = true;
};
但是,我尝试使用Linq进行相同的操作,但是未能将其写为一行.这有可能吗?我不喜欢以上述方式进行迭代的原因是,我已经在该代码块中迭代了整个列表,并认为可能会有更漂亮的方法:)
I, however, try to do the same by using Linq, but failed in writing it as one line. Is this even possible? The reason why I don't like to iterate in the way above is that I already iterate the whole list in this codeblock and think, that there might be a more beautiful way :)
推荐答案
您不应尝试将所有内容简化为一行-就像可读性一样简短.在这种情况下,您可以使用:
You shouldn't try to get everything down to one line - just as brief as is readable. In this case, you can use:
foreach (var project in MyProjects.Projects.Where(p => p.Name == "Overhead"))
{
project.IsActive = true;
}
那是在 querying 部分使用LINQ,这很合适,因为这就是LINQ的代表.我强烈敦促您不要以Mayank的答案那样对LINQ调用中的项进行突变.这很容易出错(如原始答案所证明的那样不起作用),并且与LINQ的精神背道而驰.
That's using LINQ for the querying part, which is appropriate as that's what the Q of LINQ stands for. I'd strongly urge you not to mutate items within LINQ calls in the way that Mayank's answer does though. It's error-prone (as evidenced by the original answer not working) and against the spirit of LINQ.
这与IMO的可读性差不多.请注意,它的功能与原始代码完全相同-如果每一项可能都是您想要的,您就无法避免某事遍历列表中的每一项更新.
That's about as readable as it gets, IMO. It does exactly the same thing as the original code, mind you - you can't avoid something iterating over every item in the list, if every item might be one you want to update.
只是为了开怀大笑,如果您真的,真的想要用最少的代码来做到这一点,则可以使用:
Just for laughs, if you really, really wanted to do it in pretty minimal code, you could use:
// DON'T USE THIS!
MyProjects.Project.Count(p => p.Name == "Overhead" && (p.IsActive = true));
在这里,我们使用&&
短路的事实来避免评估赋值(p.IsActive = true
),除非条件匹配.为属性分配bool
值很方便,因为这意味着我们无需执行任何其他操作即可使它成为&&
运算符的有效第二个操作数.我们使用Count()
来完全评估结果,而无需创建任何其他列表等-并且我们使用带有谓词的版本来避免甚至不需要以前版本进行的Where
调用. (LastOrDefault
也可以.)但这都是一种可怕的滥用方式,绝不应该出现在任何真实的代码中.
Here we use the fact that &&
is short-circuiting to avoid evaluating the assignment (p.IsActive = true
) unless the condition is matched. It's handy that we're assigning a bool
value to a property, as that means we don't need to do anything else to make it a valid second operand for the &&
operator. We use Count()
to fully evaluate the result without creating any additional lists etc - and we use the version with a predicate to avoid even needing a Where
call, which a previous version did. (LastOrDefault
would work too.) But it's all a horrible abuse, and should never appear in any real code.
这篇关于C#:更新列表< of>中的项目值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!