设置List <dynamic>中的值不起作用

时间:2019-04-28 19:18:28

标签: c# dynamic core

我为我的大问题写了一段代码,其中我在读取和格式化它后在同一个对象中设置了一个值。从对象位置读取该值,但未设置该值。

我正在基于索引和属性访问对象。

class Student
{
    public string City { get; set; }
    public double value1 { get; set; }
    public double value2 { get; set; }
}

以上是用于初始化列表的POCO。下面是存在问题的代码

class ClassTest
{
    public static List<dynamic> GetUpdatedListCurrency(string[] CurrecyColumns, List<dynamic> dynamicList)
    {
        int Count = CurrecyColumns.Count();

        string first = CurrecyColumns.First();
        string two = CurrecyColumns.Skip(1).First();

        for (int i = 0; i < dynamicList.Count; i++)
        {
            var t = ((double)(dynamicList[i][first])).ToString("N", CultureInfo.CreateSpecificCulture("en-US"));
            var tt = ((double)(dynamicList[i][two])).ToString("N", CultureInfo.CreateSpecificCulture("en-US"));

            //Formatted value is availble at t and tt but it is not set back to that object

            dynamicList[i][first] = t;
            dynamicList[i][two] = tt;
        }

        return dynamicList;
    }

    static void Main()
    {
        var list = new List<Student>()
        {
            new Student() { City="Noida", value1 = 44412, value2 = 33341 },
            new Student() { City="Delhi", value1 = 11212, value2 = 3421 }
        };

        var converter = new Newtonsoft.Json.Converters.ExpandoObjectConverter();
        dynamic expandoObjectList = JsonConvert.DeserializeObject<List<ExpandoObject>>(JsonConvert.SerializeObject(list), converter);

        string Columns = " value1 as value3, value2 as value4";

        var res = ((List<ExpandoObject>)expandoObjectList)
                       .ToDynamicList().AsQueryable()
                       .Select("new (" + Columns + ")")
                       .ToDynamicList().ToList();

        string str = "value3,value4";
        var updatedList = GetUpdatedListCurrency(str.Split(','), res);

    }
}

1 个答案:

答案 0 :(得分:0)

好的。所以最后我以其他解决方案浏览了它,但是我仍然不清楚为什么上面不应该设置该值。欢迎任何深入的人详细回答。以下是我的工作代码。我使用索引访问了动态列表并设置了值。

Object.getOwnPropertyNames(o)