C# 尝试使用C输出IOrderedEnumerable列表#
我已经读入了一个.csv文件,做了一些格式化,将每一行分隔成它的列,并将生成的数组添加到列的数组列表中。接下来,我使用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
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;谢谢。我不知道它们不能被索引。