Archivos en el directorio ordenados por nombre de archivo ascendente
Frecuentes
Visto 21,785 veces
3
I am having a list of files from a directory and I want to sort it out by filename.
Este es el código principal:
var localPath = this.Server.MapPath("~/Content/Img/" + type + "/");
var directory = new DirectoryInfo(localPath);
isDirectory = directory.Exists;
if (isDirectory)
{
foreach (FileInfo f in directory.GetFiles())
{
Picture picture = new Picture();
picture.ImagePath = path;
picture.CreationDate = f.CreationTime;
picture.FileName = f.Name;
listPictures.Add(picture);
}
}
here is the class Picture where all the files are stored:
public class Picture
{
public string ImagePath { get; set; }
public string FileName { get; set; }
public DateTime CreationDate { get; set; }
}
How do you do to sort a files list by order of FileName?
5 Respuestas
9
Simply change your for loop :
foreach (FileInfo f in directory.GetFiles().OrderBy(fi=>fi.FileName))
{
}
Alternatively, you can rewrite the whole loop using this code :
var sortedFiles = from fi in directory.GetFiles()
order by fi.FileName
select new Picture { ImagePath = path, CreationDate = f.CreationTime, FileName = f.FileName };
listPictures.AddRange(sortedFiles);
Respondido 28 ago 12, 09:08
2
listPictures = listPictures.OrderBy(x => x.FileName).ToList();
Respondido 28 ago 12, 09:08
2
You can use lambda expression and/or extension methods. For example:
listPictures.OrderBy(p => p.FileName).ToList();
Respondido 28 ago 12, 09:08
1
Tenga en cuenta que enumerar archivos performs lazy loading and can be more efficient for larger directories, so:
dir.EnumerateFiles().OrderBy(f => f.FileName))
Respondido 28 ago 12, 09:08
I may be wrong, but since you're calling OrderBy
, I believe Linq will have to process the whole collection before returning the first elements. Therefore, lazy loading won't have any impact. - kevin gosse
Tenga en cuenta que Directory.EnumerateFiles(path)
will be more efficient if only the FileName
se utiliza. - Tim Schmelter
@KooKiz: Yes, but using EnumerateFiles
has the advantage that you can change the query to: dir.EnumerateFiles().Where(f=>f.CreationTime>=DateTime.Now.AddAyears(-1)).OrderBy(f=>f.FileName)
without remembering to use now EnumerateFiles
en lugar de GetFiles
. So why not using it from the beginning? - Tim Schmelter
1
You can use LINQ from the beginning:
var files = from f in directory.EnumerateFiles()
let pic = new Picture(){
ImagePath = path;
CreationDate = f.CreationTime;
FileName = f.Name;
}
orderby pic.FileName
select pic;
Tenga en cuenta que Directory.EnumerateFiles(path)
will be more efficient if only the FileName
se utiliza.
Respondido 28 ago 12, 13:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# .net linq sql-order-by or haz tu propia pregunta.
why downvotes? The question seems legitimate to me - Steve B
@SteveB: I think due total simplicity of the question which can be googled or even searched over SO itself, there will be tons of answers! - abatishchev
Esto ha sido debated on meta. Not sure if this question falls into acceptable or non acceptable question. - Steve B