1

所以,基本上我需要用数组而不是手动填充这个当前列表:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<People> people = new List<People>()
            {
                new People{Name="John",Age=21,Email="john@abc.com"}
                new People{Name="Tom",Age=30,Email="tom@abc.com"}
            };
        }
    }

到目前为止,我有这样的人:

    class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Email { get; set; }
    }

我想改用数组,从 People 类到 MainWindow 类。就像是:

    class People
    {
        public string[] Name =
        {
            "John",
            "Tom",
        };
        public int[] Age =
        {
            "21",
            "30",
        }
        ...
    }

我似乎无法弄清楚如何使用这些数组来填充这个列表。提前感谢您的宝贵时间。

4

3 回答 3

2
int c = people.Name.Count;

Enumerable.Range(0,c).Select(i => new People(){Name = people.Name[i], Age = people.Age[i], ...});

您需要确保所有数组都具有相同的大小。

如果您将“数据”设置为静态会更好。

我们可以做int c = Math.Min(people.Name.Count, people.Age.Count, ...);一个更好的“对称”

于 2013-01-24T16:30:59.347 回答
0

其中一个Select重载有index作为参数

var names = new[]{ "John", "Tom" };
var ages  = new[]{ 21, 30, };
var mails = new[]{ "mail1", "mail2", };

names.Select((name, index) => 
       new People{
       Name = name, 
       Age = ages[index], 
       Email = mails[index]})
     .Dump();

印刷:

Name Age Email 
John 21  mail1 
Tom  30  mail2 
于 2013-01-24T16:27:41.720 回答
0

有很多方法可以做到这一点,如果这是预填充集合

public static class PeopleListFactory
{
   public static IEnumerable<People> Content(int someindicator)
   {
      List<People> result = new List<People>();
      switch (someIndicator)
      {      
        case 0: // fill it in here with and from whatever you like.
        break;
      }
      return result;
   }
}

然后在你的主要形式中

列出人员=新列表(PeopleListFactory.Content(0));

于 2013-01-24T16:37:36.230 回答