基于对象变量对象的搜索列表 [英] Search list of objects based on object variable

查看:97
本文介绍了基于对象变量对象的搜索列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的列表。这些对象有三个变量,ID,名称,和放大器;值。可以有很多在这个列表中的对象,我需要找到基于ID或名称之一,并更改该值。
示例

I have a list of objects. These objects have three variables, ID, Name, & value. There can be a lot of objects in this list, and I need to find one based on the ID or Name, and change the value. Example

class objec
{
    public string Name;
    public int UID;
    public string value;
}
List<objec> TextPool = new List<objec>();



我怎么会发现在TextPool一个条目具有测试的名称并改变其值到值。
真正的程序有更多的搜索选项,以及需要改变的值,所以我不能只用一个词典(虽然名称和UID或唯一标识符)。
任何帮助将是巨大的。

How would I find the one entry in TextPool that had the Name of 'test' and change its value to 'Value'. The real program has many more search options, and values that need changing, so I couldn't just use a Dictionary (though Name and UID or unique identifiers). Any help would be great

推荐答案

您可以使用LINQ找到它,然后直接更改的元素:

You could use LINQ to find it, then change the element directly:

var item = TextPool.FirstOrDefault(o => o.Name == "test");
if (item != null)
       item.value = "Value";

如果你想改变相匹配的所有元素,你可以潜在的,甚至做的:

If you wanted to change all elements that match, you could, potentially, even do:

TextPool.Where(o => o.Name == "test").ToList().ForEach(o => o.value = "Value");



不过,我个人宁可拆它,因为我觉得第二个选项是少维护的(做其中直接在查询结果闻香给我)...

However, I personally would rather split it up, as I feel the second option is less maintainable (doing operations which cause side effects directly on the query result "smells" to me)...

这篇关于基于对象变量对象的搜索列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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