C# 数组/数组列表

C# 数组/数组列表,c#,arrays,C#,Arrays,我对C相当陌生# 我试图从外部数据源中检索一些信息并将其存储在数组中,一旦它位于数组中,我希望按时间对其进行排序 我知道如何对一行中的一列执行此操作,但是我需要的信息有多列 例如: foreach (Appointment Appoint in fapts) { // Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array } // So

我对C相当陌生#

我试图从外部数据源中检索一些信息并将其存储在数组中,一旦它位于数组中,我希望按时间对其进行排序

我知道如何对一行中的一列执行此操作,但是我需要的信息有多列

例如:

foreach (Appointment Appoint in fapts)
{
    // Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
} 

// Sort my array by Appoint.Start

foreach ( item in myNewArray ) 
{
    //print out Appoint.Subject - Appoint.Start, Appoint.Organiser.Name.ToString() and Appoint.location
}
非常感谢你的帮助

编辑:

我有多个数据源,可用于:

foreach (Appointment Appoint in fapts)
{
    // Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
} 

因此需要在一个新数组中对项目进行排序,我知道这不是很有效,但无法通过任何其他方式获得我需要的信息。

如果数据概念上是一行多列的,请考虑使用Dictionary对象而不是数组

foreach(KeyValuePair<string, string> entry in MyDic)
{
    // do something with entry.Value or entry.Key
}
foreach(MyDic中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}

如果数据在概念上是一行多列,则考虑使用字典对象而不是数组

foreach(KeyValuePair<string, string> entry in MyDic)
{
    // do something with entry.Value or entry.Key
}
foreach(MyDic中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}

您有多种选择。最常见的两种是:

1) 创建一个类,然后定义该类的数组或列表,并填充该类

2) 创建与数据格式匹配的结构,并创建该结构的数组或列表

当然,您可以将数据转换为XML格式或数据集,但这可能比您需要做的工作更多

public List<foo> appointments = new List<foo>(); 
public struct foo
{
public string subject ;
public DateTime start ;
public string name ;
public string location ;
}
public void foo1()
{
// parse the file
while (!File.eof())
    {
        // Read the next line...
        var myRecord = new foo() ;
        myRecord.subject = data.subject ;
        myRecord.start = data.Start ;
        myRecord.name = data.Name ;
        //...
        appointments.Add(myRecord);
    }
}
公共列表约会=新列表();
公共结构foo
{
公共字符串主题;
公共日期时间开始;
公共字符串名称;
公共字符串位置;
}
公共图书馆1(
{
//解析文件
而(!File.eof())
{
//读下一行。。。
var myRecord=newfoo();
myRecord.subject=data.subject;
myRecord.start=data.start;
myRecord.name=data.name;
//...
任命。添加(我的记录);
}
}
享受


(因为我无法对评论进行评论和回复-不清楚他是否有课程等,或者只是向我们展示他想做什么。我假设这只是为了演示,因为没有任何关于如何读取数据的信息。如果他已经可以将其放入一个类中,那么第一个答案无论如何都适用。我只是扔掉了la。)因为它们是先获取数据的选项。)

您有很多选项。最常见的两个选项是:

1) 创建一个类,然后定义该类的数组或列表,并填充该类

2) 创建与数据格式匹配的结构,并创建该结构的数组或列表

当然,您可以将数据转换为XML格式或数据集,但这可能比您需要做的工作更多

public List<foo> appointments = new List<foo>(); 
public struct foo
{
public string subject ;
public DateTime start ;
public string name ;
public string location ;
}
public void foo1()
{
// parse the file
while (!File.eof())
    {
        // Read the next line...
        var myRecord = new foo() ;
        myRecord.subject = data.subject ;
        myRecord.start = data.Start ;
        myRecord.name = data.Name ;
        //...
        appointments.Add(myRecord);
    }
}
公共列表约会=新列表();
公共结构foo
{
公共字符串主题;
公共日期时间开始;
公共字符串名称;
公共字符串位置;
}
公共图书馆1(
{
//解析文件
而(!File.eof())
{
//读下一行。。。
var myRecord=newfoo();
myRecord.subject=data.subject;
myRecord.start=data.start;
myRecord.name=data.name;
//...
任命。添加(我的记录);
}
}
享受


(因为我无法对评论进行评论和回复-不清楚他是否有课程等,或者只是向我们展示他想做什么。我假设这只是为了演示,因为没有任何关于如何读取数据的信息。如果他已经可以将其放入一个类中,那么第一个答案无论如何都适用。我只是扔掉了la。)st 2在那里,因为它们是先获取数据的选项。)

不清楚您的最终目标是什么,但:

使用通用的
列表
而不是数组:

有关为什么首选使用
列表
的更多信息,请参见此

List<Appointment> appointments = new List<Appointment>();

foreach (Appointment Appoint in fapts)
{
    appointments.Add(Appoint);
} 

foreach (var item in appointments) 
{
    Console.WriteLine(item.Subject);
    Console.WriteLine(item.Foo);
    // Here you could override ToString() on Appointment to print eveything in one Console.WriteLine
}

不清楚您的最终目标是什么,但:

使用通用的
列表
而不是数组:

有关为什么首选使用
列表
的更多信息,请参见此

List<Appointment> appointments = new List<Appointment>();

foreach (Appointment Appoint in fapts)
{
    appointments.Add(Appoint);
} 

foreach (var item in appointments) 
{
    Console.WriteLine(item.Subject);
    Console.WriteLine(item.Foo);
    // Here you could override ToString() on Appointment to print eveything in one Console.WriteLine
}

您可以使用排序运算符和对列表进行排序,如下所示

using System.Linq;
然后

var appointments = new List<Appointment>();
var sortedAppointments = list.OrderBy(l => l.Subject).ThenBy(l => l.Name).ToList();
var约会=新列表();
var sortedappoints=list.OrderBy(l=>l.Subject).ThenBy(l=>l.Name.ToList();

这将创建一个新的约会列表,按主题排序,然后按名称排序。

您可以使用排序运算符和对列表进行排序,如下所示

using System.Linq;
然后

var appointments = new List<Appointment>();
var sortedAppointments = list.OrderBy(l => l.Subject).ThenBy(l => l.Name).ToList();
var约会=新列表();
var sortedappoints=list.OrderBy(l=>l.Subject).ThenBy(l=>l.Name.ToList();

这将创建一个新的约会列表,先按主题排序,然后按名称排序。

如果fpts中已有对象列表,请对该列表本身进行排序:

fpts.OrderBy(x => x.Subject).ThenBy(x => x.Location).ToList();

fpts中已经有一个对象列表,请对该列表本身进行排序:

fpts.OrderBy(x => x.Subject).ThenBy(x => x.Location).ToList();

林克是你的朋友

fapts
似乎已经是一个集合,因此您可以对其进行操作

var myNewArray = fapts.OrderBy(Appoint => Appoint.Start).ToArray()
我使用了
ToArray()
调用来强制立即求值,这意味着myNewArray已经被排序,因此如果您多次使用它,就不必重新求值排序

或者,如果只使用了一次,那么很容易就会错过ToArray()部分,然后排序的执行将被推迟,直到您尝试通过myNewArray枚举为止

此解决方案将源对象放入数组中,但如果您只是想存储您提到的特定字段,则需要使用select。数组项类型有两种选择,您可以使用匿名类,如果您从函数返回此数组,则会遇到困难,也可以定义类

对于匿名:

 var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
                       .Select(Appoint => new {
                                Start = Appoint.Start,
                                Organiser = Appoint.Organiser.Name.ToString(),
                                Location = Appoint.Location 
                               }).ToArray();
对于命名类,假设类为
MyClass

 var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
                       .Select(Appoint => new MyClass {
                                Start = Appoint.Start,
                                Organiser = Appoint.Organiser.Name.ToString(),
                                Location = Appoint.Location 
                               }).ToArray();

林克是你的朋友

fapts
似乎已经是一个集合,因此您可以对其进行操作

var myNewArray = fapts.OrderBy(Appoint => Appoint.Start).ToArray()
我使用了
ToArray()
调用来强制立即求值,这意味着myNewArray已经被排序,因此如果您多次使用它,您就不会进行排序