score: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);
score:2
listPictures = listPictures.OrderBy(x => x.FileName).ToList();
score:2
You can use lambda expression and/or extension methods. For example:
listPictures.OrderBy(p => p.FileName).ToList();
score:1
Note that EnumerateFiles performs lazy loading and can be more efficient for larger directories, so:
dir.EnumerateFiles().OrderBy(f => f.FileName))
score: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;
Note that Directory.EnumerateFiles(path)
will be more efficient if only the FileName
is used.
Source: stackoverflow.com
Related Articles
- Files in directory sort by fileName ascending
- Whats the best way to search files in a directory for multiple extensions and get the last write time according to filename
- C#: Get the 5 newest (last modified) files from a directory
- Recursively delete files from a Directory but keeping the dir structure intact
- How to sort files by date in file name using c#?
- Sort part of a list in descending order (by date), the other part in ascending order (alphabetically)?
- LINQ Source Code Available
- Trying to get a list of files in a directory by created date using LINQ
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- How to filter files from directory using LINQ?
- Find duplicate files in a directory using LINQ
- LINQ: Sort records in ascending order with NULL values last
- Merge files in a directory using Linq to Objects
- Ascending sort date
- How to Sort this String in Ascending Alphanumeric
- In C# matching all files in a directory using regex
- How do I get the files from Directory in order in asp.net?
- how to sort a list in ascending order LINQ
- source code for LINQ 101 samples
- Sort List with two parameters but in different lines of code
- Linq to get a list of files by ascending order from a directory, based on their name C#
- How to sort list on multiple properties in one line of code in Linq
- Get only those files in a directory whose name does not start with a certain string
- How to remove files from a directory if they don't contain the list of specified files provided?
- LINQ or Lamba to sort an array of integers in ascending order within the set of their frequency of repetition
- Using c# GetFiles Length but only count the files with certain amount of chars in filename
- Split list by one property and sort ascending nested list<T> by property
- How to get all xml files in a directory that satisfy attribute
- List or Array of String Contain specific word in Html Source Code
- Custom OrderBy on a List of List
- LINQ: take a sequence of elements from a collection
- Return date records to nearest minute using linq
- Dynamically Create LINQ Statements
- Join two M:N relations in EntityFramework
- How can i join two table in Linq and display the join to Data GridView?
- Web Service taking object as parameters
- using PredicateBuilder inside a select
- LINQ filtering between two times and by hour
- How can I determine if an entity collection contains one of several possible values?
- Grouping words according to their lengths c#
- LINQ statement, select field that has specific attribute
- Group data in DataTable
- Remove duplicated elements List<Vector3> Mono / C# - Unity3D
- How can I filter results of one LINQ query based on another?
- Get a UserControl with a certain interface inside a hierarchy
- Combine several Lists into one, GroupBy certain column, convert each Grouped result into HTML table
- How do you skip default value columns on insert/update in Linq To SQL?
- Unique ticket numbers for software support system
- How to flatten tree via LINQ?