首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新对象列表中具有特定值的所有字段

如何更新对象列表中具有特定值的所有字段
EN

Stack Overflow用户
提问于 2017-06-26 17:22:10
回答 2查看 3.8K关注 0票数 0

我有一个对象列表,我想将所有"null“字段更新为string.empty。

以下是代码:

代码语言:javascript
复制
public class Class1
{
    public string P1 { get; set; }
    public string P2 { get; set; }
    public string P3 { get; set; }
}

我希望有一个代码,它在所有字段中查找所有空值,并将值更改为string.empty。

代码语言:javascript
复制
     static void Main(string[] args)
    {

        var list= new List<Class1>();
        var class1 = new Class1 {P1 = "P1-1", P2 = "P2-1", P3="null"};
        list.Add(class1);
        var class2 = new Class1 { P1 = "P1-2", P2 = "P2-2", P3 = "null" };
        list.Add(class2);

    }

因此,我需要找到class1.P3和class2.P3,并替换它们的值。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-26 17:38:00

您可以编写这样一个简短的泛型函数:

代码语言:javascript
复制
private static IEnumerable<TSource> ReplaceValues<TSource>(IEnumerable<TSource> source, object oldValue,
    object newValue)
{
    var properties = typeof(TSource).GetProperties();
    foreach (var item in source)
    {
        foreach (var propertyInfo in properties.Where(t => Equals(t.GetValue(item), oldValue)))
        {
            propertyInfo.SetValue(item, newValue);
        }
        yield return item;
    }
}

这比您的更有效,因为您的集合类型是TSource,这意味着内部的所有类型都具有相同的属性。获取和缓存这些属性将加快进程,因为您只调用Type.GetProperties()一次,而不是操作和过滤这些结果。

更新

正如下面关于Ivan Stoev的注释部分所讨论的那样,让方法只修改集合而不返回任何值会更合适:

代码语言:javascript
复制
private static void ReplaceValues<TSource>(IEnumerable<TSource> source, object oldValue,
    object newValue)
{
    var properties = typeof(TSource).GetProperties();
    foreach (var item in source)
    {
        foreach (var propertyInfo in properties.Where(t => Equals(t.GetValue(item), oldValue)))
        {
            propertyInfo.SetValue(item, newValue);
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-06-26 17:23:12

它会处理好的:

代码语言:javascript
复制
   static void Main(string[] args)
    {

        var list= new List<Class1>();
        var class1 = new Class1 {P1 = "P1-1", P2 = "P2-1", P3="null"};
        list.Add(class1);
        var class2 = new Class1 { P1 = "P1-2", P2 = "P2-2", P3 = "null" };
        list.Add(class2);


        foreach (var item in list)
        {
            var props2 = from p in item.GetType().GetProperties()
                        let attr = p.GetValue(item)
                        where attr != null && attr.ToString() == "null"
                        select p;

            foreach (var i in props2)
            {
                i.SetValue(item, string.Empty);
            }
        }
    }

最新情况::

下面是一种更有效的方法。

代码语言:javascript
复制
    static void Main(string[] args)
    {

            var list = new List<Class1>();
            var class1 = new Class1 {P1 = "P1-1", P2 = "P2-1", P3 = "null"};
            list.Add(class1);
            var class2 = new Class1 {P1 = "P1-2", P2 = "P2-2", P3 = "null"};
            list.Add(class2);



           var updatedList=  ReplaceValues(list, "null", string.Empty);

    }

    private static IEnumerable<TSource> ReplaceValues<TSource>
    (IEnumerable<TSource> source, object oldValue,
    object newValue)
    {
        var properties = typeof(TSource).GetProperties();
        var sourceToBeReplaced = source as TSource[] ?? source.ToArray();
        foreach (var item in sourceToBeReplaced)
        {
            foreach (var propertyInfo in properties.Where(t => Equals(t.GetValue(item), oldValue)))
            {
                propertyInfo.SetValue(item, newValue);
            }
        }

        return sourceToBeReplaced;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44765192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档