Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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中用两列对csv文件进行排序#_C#_Linq_Sorting_Csv - Fatal编程技术网

C# 如何在c中用两列对csv文件进行排序#

C# 如何在c中用两列对csv文件进行排序#,c#,linq,sorting,csv,C#,Linq,Sorting,Csv,我的csv文件有14列和大约800.000行。我必须先按第10列排序,然后按第3列排序。 我使用下面的代码,但只按第10列排序 string filePath = "D:\\csv.csv"; string[] lines = File.ReadAllLines(filePath, Encoding.Default); var data = lines.Skip(1); var sorted =

我的csv文件有14列和大约800.000行。我必须先按第10列排序,然后按第3列排序。 我使用下面的代码,但只按第10列排序

            string filePath = "D:\\csv.csv";

            string[] lines = File.ReadAllLines(filePath, Encoding.Default);

            var data = lines.Skip(1);
            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).Select(x => x.Line);
            File.WriteAllLines("D:\\sortedCsv.csv", lines.Take(1).Concat(sorted), Encoding.Default);
            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
我的csv喜欢

            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
  • col1;col2;col3;。。。。。。;col10
  • abc;fds;123456 ;.... ;123 ; ..
  • def;dsa;12435 ; .... 124 ; ..
您需要在第一次订购后使用thenBy

            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
试试这个:

            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
var sorted = data.Select(line => new
        {
            SortKey = Int32.Parse(line.Split(';')[9]),
            SortKey2 = line.Split(';')[2],
             Line = line

        }
        ).OrderBy(x => x.SortKey).ThenBy(x=>x.SortKey2).Select(x => x.Line);

基本上,添加第二个排序标准,然后按指定的顺序排序。

您必须使用
OrderBy(…)。然后按(…)

            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
请注意,在这种情况下,
File.ReadLines
File.ReadAllLines
更有效

            var sorted = data.Select(line => new
            {
                SortKey = Int32.Parse(line.Split(';')[9]),
                 Line = line

            }
            ).OrderBy(x => x.SortKey).ThenBy(x => x.Line);