更新时间:2023-11-24 14:23:40
问题在你的 OrderBy(n => n),是alpha排序。
您需要将 n 转换为num值。
尝试排序类似 Convert.ToInt32(n)而不是 n
如果你的文件名只包含数值那么试试这个 -var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy(n = > Convert.ToInt16(Path.GetFileNameWithoutExtension(n)));
否则你可以根据这里分享的逻辑编写一个函数
http://***.com/ a / 5093939/1006297 [ ^ ]var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy(n = > PadNumericPortion(n));
希望,它有帮助:)
我写了这段代码现在我正确得到var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy( F => int.Parse(Path.GetFileNameWithoutExtension(F)));
HI
I want to sort files in a directory containing .sql files named as
1.sql
2.sql
3.sql
....
....
....
n.sql
I want to sort filed by number
I have written this code
string sqlConnectionString = cn.ConnectionString.ToString();
SqlConnection conn = new SqlConnection(sqlConnectionString);
string script = string.Empty;
Server server = new Server(new ServerConnection(conn));
MessageBox.Show(server.Information.Version.ToString());
bool IsFailed = false;
string mappath = Path.Combine(Application.StartupPath.ToString(), "Scripts");
//var files1 = from file in Directory.GetFiles(mappath)
// orderby file
// select file;
//var biggest = files1.First();
//var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f => new FileInfo(f).Name);
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => n);
string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.sql");
Would you please resolve this error
The problem is in your OrderBy(n => n), is does an alpha sort.
You need to convert the n to the num value.
Try to sort on something like Convert.ToInt32(n) rather than on n
If your file names contains only numeric values then try this-var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => Convert.ToInt16(Path.GetFileNameWithoutExtension(n)));
Else you can write a function based on the logic shared here
http://***.com/a/5093939/1006297[^]var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => PadNumericPortion(n));
Hope, it helps :)
I have written this code Now it is getting Correctlyvar files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f=>int.Parse(Path.GetFileNameWithoutExtension(f)));