从嵌套List中提取数据

时间:2013-02-05 09:57:12

标签: c# asp.net .net list

守则:

List<Expense> exp = new List<Expense>();
List<Budget> bud = new List<Budget>();

     bud.Add(new Budget()
     {
         sal_tp = 0,
         sal_fos_veri = 1
     });


     exp.Add(new Expense()
       {
             sal_tp =2,                                             
             sal_fos_veri = 3                            
       });
bud.Add(new Budget()
     {
         sal_tp = 4,
         sal_fos_veri = 5
     });


     exp.Add(new Expense()
       {
             sal_tp =6,                                             
             sal_fos_veri = 7                            
       });


bud.Add(new Budget()
     {
         sal_tp = 8,
         sal_fos_veri = 9
     });


     exp.Add(new Expense()
       {
             sal_tp =10,                                             
             sal_fos_veri = 11                            
       });


bud.Add(new Budget()
     {
         sal_tp = 12,
         sal_fos_veri = 13
     });


     exp.Add(new Expense()
       {
             sal_tp =14,                                             
             sal_fos_veri = 15                            
       });


bud.Add(new Budget()
     {
         sal_tp = 16,
         sal_fos_veri = 17
     });


     exp.Add(new Expense()
       {
             sal_tp =18,                                             
             sal_fos_veri = 19                            
       });


bud.Add(new Budget()
     {
         sal_tp = 20,
         sal_fos_veri = 21
     });


     exp.Add(new Expense()
       {
             sal_tp =22,                                             
             sal_fos_veri = 23                            
       });


bud.Add(new Budget()
     {
         sal_tp = 24,
         sal_fos_veri = 25
     });


     exp.Add(new Expense()
       {
             sal_tp =26,                                             
             sal_fos_veri = 27                            
       });

部分课程结束

public class Expense
                    {
                        public int sal_tp { get; set; }
                        public int sal_fos_veri { get; set; }
                    }
            public class Budget
                {
                    public int sal_tp { get; set; }
                    public int sal_fos_veri { get; set; }
                  }

期待输出如下:

* 0 2 1 4 3 5 ....等等.... *

  

如何一次迭代两个列表?我尝试过类似的东西   此

for(int i=0;i<bud.Count;i++)
{
                       Expense explist=exp[i];
                       Budget budlist=bud[i];
                       Response.Write(budlist.sal_tp);
                       Response.Write(explist.sal_tp);
                       Response.Write(budlist.sal_fos_veri);
                       Response.Write(explist.sal_fos_veri);
 }

问题:这会增加代码,我想迭代一遍“budlist”。就像我曾经在java中做过类似的事情

for(int i=0;i<data.size();i++)
       {
          for(int j=0;j<((ArrayList)data.get(i)).size();j++)
           {
               out.print("<td>");
               out.print(((ArrayList)data.get(i)).get(j));
               out.print("</td>");
               out.print("<td>"+((ArrayList)exp_data.get(i)).get(j)+"</td>");
           }
           out.println("</tr>");
       }

有可能吗?

1 个答案:

答案 0 :(得分:1)

尝试将常见抽象提取为

public interface  IMoneyTransaction
{
    int sal_tp { get; set; }
    int sal_fos_veri { get; set; }
}

你的班级声明会有所改变:

public class Expense : IMoneyTransaction
{
    public int sal_tp { get; set; }
    public int sal_fos_veri { get; set; }
}

public class Budget : IMoneyTransaction
{
    public int sal_tp { get; set; }
    public int sal_fos_veri { get; set; }
}

将列表填写为:

var exp = new List<IMoneyTransaction>{
    new Budget
    {
        sal_tp = 0,
        sal_fos_veri = 1
    }};

var bud = new List<IMoneyTransaction>{
    new Expense
    {
        sal_tp =2,                                             
        sal_fos_veri = 3                            
    }};

现在,您可以使用Union合并数据并使用一个通用接口迭代它

var allItems = exp.Union(bud).ToArray();

for(int i = 0; i < allItems.Length; i++)
{
    Console.WriteLine (allItems[i].sal_fos_veri);
    Console.WriteLine (allItems[i].sal_tp);
}