简体   繁体   English

如何在C#中更新列表集合中的元素

[英]How to update a element in List collection in C#

I have a List collection like List<clsTest> objList = new List<clsTest>(); 我有一个List集合,例如List<clsTest> objList = new List<clsTest>();

It has around hundred columns. 它有大约一百列。

Predicate<ScreenDefinition> linqQuery = new Predicate<ScreenDefinition>(objQuery);
index = tmp.FindIndex(objQuery);

objQuery is query for conditions objQuery是查询条件

I tried like for update 我尝试过更新

listJoinedScreenDefinition[index].fieldName = "update";

But prob is i need to genrate 'fieldName' dynamically according to my logic. 但是问题是我需要根据我的逻辑动态生成“ fieldName”

Say "seat" + 05 => seat05 is the column name.

Like wise seat1 - seat 100 are there. 就像明智的座位1-座位100一样。

In every iteration i need to update only one column. 在每次迭代中,我只需要更新一列。

in the way i tried how to pass the dynamically column name in that List ? 以我尝试如何在该列表中传递动态列名的方式?

I am new to .Net and Linq. 我是.Net和Linq的新手。

Please suggest some way to achieve this.. 请提出一些实现此目标的方法。

Your question has nothing to do with lists and queries. 您的问题与列表和查询无关。 You would be in the same situation if you had only one instance of your clsTest class. 如果只有clsTest类的一个实例,您将处于相同的情况。 The problem is with the design of the class. 问题在于类的设计。

If I understand you correctly, clsTest has 100 members, with names like seat1 , seat2 , seat50 , etc, is that correct? 如果我对您的理解正确,那么clsTest有100个成员,其名称如seat1seat2seat50等,对吗? If so, that's a pretty bad class design, as you've found out, because it requires you to hardcode all references to specific fields inn your code, or alternately use reflection (as Garath suggested in his answer) which is is more complicated and error prone. 如果是这样,那是一个非常糟糕的类设计,正如您所发现的那样,因为它要求您对代码中特定字段的所有引用进行硬编码,或者交替使用反射(如Garath在其回答中建议的那样),这更加复杂且容易出错。

A better solution would be to redesign your clsTest class to have a property called Seats which is an Array or List of seats. 更好的解决方案是重新设计clsTest类,使其具有一个名为Seats的属性,该属性是ArraySeats List If you have a public List<string> Seats {get;set;} , you can access myObject.Seats[5] to get to the fifth seat. 如果您有一个public List<string> Seats {get;set;} ,则可以访问myObject.Seats[5]进入第五个座位。

Of course, if it's more than just a list of seats, and you can have several different types of numbered properties ("seat" + "05" and "row" + "02", for instance), you can use a Dictionary<string, string> , to map between "seat05" and its value. 当然,如果不仅仅是一个座位列表,而且您可以使用几种不同类型的编号属性(例如,“ seat” +“ 05”和“ row” +“ 02”),则可以使用Dictionary<string, string> ,以在“ seat05”及其值之间进行映射。

The easiest way is to using reflection. 最简单的方法是使用反射。 I do not know if you are using properties or fields. 我不知道您是否使用属性或字段。 For properties code will look like: 对于属性代码将如下所示:

var target = listJoinedScreenDefinition[index];
Type type = target.GetType();
PropertyInfo prop = type.GetProperty("propertyName");
prop.SetValue (target, propertyValue, null);

For fields code is very similar: 对于字段,代码非常相似:

var target = listJoinedScreenDefinition[index];
FieldInfo fi = target.GetType().GetField("Name", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(target ,value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM