【问题标题】:How can I get class Properties values saved in a List of objects and Iterate using a loop如何获取保存在对象列表中的类属性值并使用循环进行迭代
【发布时间】:2023-04-11 05:06:03
【问题描述】:

这是我的课

public class Income
    {
        public string Code { get; set; }
        public int Month1 { get; set; }
        public int Month2 { get; set; }
        public int Month3 { get; set; }
        public int Month4 { get; set; }
        public int Month5 { get; set; } 
        public List<Income> income { get; set; }

    }

在另一个班级

List<Income> incomeList = new List<Income>();

//repeat twice
Income obj = new Income();
obj.Month1 = 200;
obj.Month2 = 150;
...
IncomeList.Add(obj);
obj.income = IncomeList;

现在我想为每个保存在 列表。 目前为止

Int[] results = obj.income
    .Select(x=> new 
    {
         x.Month1,
         x.Month2,
         x.Month3,
         x.Month4,
         x.Month5
     })
     .ToArray();

这是我需要为每个唯一对象添加总月数的地方。 获取所有 Months1 、 Months2 的总数 ...

double totals[] = new double[5];
 for (int i=0;i<results.length;i++)
 {
     totals[i] += results[i]; // I get the first object reference
     // I want Moth1,Month2 ... to be in an indexed array where 
     // if i want Month1 i would access similar to : results[index];
 }

【问题讨论】:

  • 不确定我是否 100% 清楚您要做什么。但是,如果您希望这 5 个属性在列表中,您是否可以简单地向该类添加一个属性,将这些属性值作为列表返回?
  • 你不想用public int[] months = new int[5]而不是这5个int MonthX吗?
  • @David 我会这样做的
  • @Gariel J 另一个类用独特的代码表示来自数据库的数据,为什么,感谢您的建议

标签: c#


【解决方案1】:

好的,下面的代码将投影出一个对象列表。每个对象都有一个名为“Months”的属性,它是月份值的int[]

这能满足你的需要吗?

void Main()
{ 

    var incomeList = new List<Income>();
    Income obj = new Income();
    obj.Month1 = 200;
    obj.Month2 = 150;

    incomeList.Add(obj);

    var results = incomeList
        .Select(x => new
        {
            Months = new int[]
            {
                x.Month1,
                x.Month2,
                x.Month3,
                x.Month4,
                x.Month5
            }
        })
     .ToArray();


    for (int i = 0; i < results.Length; i++)
    {
        var testResults = results[i];
        Console.WriteLine($"Month 1: {testResults.Months[0]}");
        Console.WriteLine($"Month 2: {testResults.Months[1]}");
        Console.WriteLine($"Month 3: {testResults.Months[2]}");
        Console.WriteLine($"Month 4: {testResults.Months[3]}");
        Console.WriteLine($"Month 5: {testResults.Months[4]}");
    }
}

但是,鉴于您发布的代码中的 cmets,我认为您想将其转换为二维数组。如果是这样,只需投射出int[]

void Main()
{ 
    var incomeList = new List<Income>();
    Income obj = new Income();
    obj.Month1 = 200;
    obj.Month2 = 150;

    incomeList.Add(obj);

    int[][] results = incomeList
        .Select(x => new int[]
        {
            x.Month1,
            x.Month2,
            x.Month3,
            x.Month4,
            x.Month5
        })
     .ToArray();


    for (int i = 0; i < results.Length; i++)
    {
        var testResults = results[i];
        Console.WriteLine($"Month 1: {testResults[0]}");
        Console.WriteLine($"Month 2: {testResults[1]}");
        Console.WriteLine($"Month 3: {testResults[2]}");
        Console.WriteLine($"Month 4: {testResults[3]}");
        Console.WriteLine($"Month 5: {testResults[4]}");
    }
}

【讨论】:

  • int[][] results = incomeList ...这工作 results[objectRef1][index0] retrive 200 。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2014-05-01
  • 2014-09-03
  • 2012-12-14
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多