Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用C输出IOrderedEnumerable列表#_C#_Arrays_Visual Studio 2010_Linq_Console Application - Fatal编程技术网

C# 尝试使用C输出IOrderedEnumerable列表#

C# 尝试使用C输出IOrderedEnumerable列表#,c#,arrays,visual-studio-2010,linq,console-application,C#,Arrays,Visual Studio 2010,Linq,Console Application,我已经读入了一个.csv文件,做了一些格式化,将每一行分隔成它的列,并将生成的数组添加到列的数组列表中。接下来,我使用IOrderedEnumerable对数组列表进行排序,以按字母顺序升序的第二列对其进行排序,然后尝试将这个新排序的列表放到屏幕上。这是我坚持的最后一部分 这就是我所尝试的: // attempt to read file, if it fails for some reason display the exception error message try { // c

我已经读入了一个.csv文件,做了一些格式化,将每一行分隔成它的列,并将生成的数组添加到列的数组列表中。接下来,我使用
IOrderedEnumerable
对数组列表进行排序,以按字母顺序升序的第二列对其进行排序,然后尝试将这个新排序的列表放到屏幕上。这是我坚持的最后一部分

这就是我所尝试的:

// attempt to read file, if it fails for some reason display the exception error message
try
{
    // create list for storing arrays
    List<string[]> users = new List<string[]>();

    string[] lineData;
    string line;

    // read in stremreader
    System.IO.StreamReader file = new System.IO.StreamReader("dcpmc_whitelist.csv");
    // loop through each line and remove any speech marks
    while((line = file.ReadLine()) != null)
    {
        // remove speech marks from each line
        line = line.Replace("\"", "");

        // split line into each column
        lineData = line.Split(';');

        // add each element of split array to the list of arrays
        users.Add(lineData);

    }

    //sort this list by username ascending
    IOrderedEnumerable<String[]> usersByUsername = users.OrderBy(user => user[0]);

    // display the newly ordered list
    for (int i = 0; i <= users.Count; i++)
    {
        Console.WriteLine(usersByUsername[i]);
    }

    // after loading the list take user to top of the screen
    Console.SetWindowPosition(0, 0);
}
catch (Exception e)
{
    // Let the user know what went wrong when reading the file
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
//尝试读取文件,如果由于某种原因失败,将显示异常错误消息
尝试
{
//创建用于存储阵列的列表
列表用户=新列表();
字符串[]行数据;
弦线;
//读入stremreader
System.IO.StreamReader文件=新的System.IO.StreamReader(“dcpmc_whitelist.csv”);
//在每一行中循环并删除任何语音标记
而((line=file.ReadLine())!=null)
{
//删除每行的语音标记
行=行。替换(“\”,“”);
//将行拆分为每列
lineData=line.Split(“;”);
//将拆分数组的每个元素添加到数组列表中
users.Add(lineData);
}
//按用户名升序排列此列表
IOrderedEnumerable usersByUsername=users.OrderBy(user=>user[0]);
//显示新排序的列表

对于(int i=0;i原因既不是
IEnumerable
也不是
IORDerenumerable
支持索引,显示错误

要显示排序结果,可以使用
foreach
枚举集合:

// display the newly ordered list
foreach (var user in usersByUsername)
{
    Console.WriteLine(string.Join(", ", user));
}
或者,您可以将结果转换为列表并使用索引:

//sort this list by username ascending
IList<String[]> usersByUsername = users.OrderBy(user => user[0]).ToList();

// display the newly ordered list
for (int i = 0; i <= users.Count; i++)
{
    Console.WriteLine(string.Join(", ", usersByUsername[i]));
}
//按用户名升序排序此列表
IList usersByUsername=users.OrderBy(user=>user[0]).ToList();
//显示新排序的列表

对于(int i=0;谢谢。我不知道它们不能被索引。