Sort two Lists together as one?

前端 未结 7 2082
执笔经年
执笔经年 2020-12-07 01:30

I have two List which I am accessing by index (e.g. Name[10], Date[10]). They\'re closely tied, so Name[10] is related to Date[1

7条回答
  •  余生分开走
    2020-12-07 02:01

    Create a class called "Result" with two properties

    public class Result
    {
      public string Name {set;get;}
      public DateTime Date { set;get;}
    }
    

    Now you may create a List of this class

    List objListResult=new List();
    

    Now in your case, If you want, you may read the content of your indidual List and put it to our new List. (This is only purticular to your case, if you really want to move data from your old Lists to our new List. If possible you may update the code where you load data to two List(that is the correct way) so that it will load data to our new List of Type Result and avoid this porting code to move data to new List.

     for(int i=0;i

    You can access the Items from the new List like this

     objListResult[2].Date   
    

    will give you the Date Stored for the second object in the list

     objListResult[2].Name
    

    will give you the Name Stored for the second object in the list

提交回复
热议问题