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: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.
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();
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- How can I use Kendo UI with Razor?
- select properties of T if they match a column name in a datatable and ignore case
- Using reflection throws error in EF Core 3
- How to load two navigation property in a single statement in order to avoid database round trips(if any)
- LINQ: Improving performance of "query to find all dictionaries from list of dictionaries where given key has at least one value from list of values"
- Entity Framework database-first approach - how to make async method?
- c# Linq Data grid view column checking if null
- DataContext in static class in desktop application
- Convert single LINQ value to string
- Conditional XElement Content, XElement or XAttribute
- Using Linq.Dynamic where method with a dynamic list gives error
- How to use a method with an out parameter in a lambda expression
- Join a string property which is in a List of objects using Linq
- Anonymous Type with Lambda Expressions
- JSON Lists not removing duplicates with Distinct command
- Use Regex in this Linq query?
- Using SQL "In" instead of "Like" using %wildcards% in Linq
- How to typecast from var to an object
- Linq to Entities 4.0 - Optimized Query and multiple calls to database in a single query
- Can I capture a local variable into a LINQ Expression as a constant rather than a closure reference?
- Writing left join Query in LINQ
- Split CSV files with header c#
- How to use subItem level using Expression?
- How to find Distinct in more than one column in LINQ
- How to sort a nested Collection by its objects modified value?
- How to get dynamic predicate using expressions when joining in Linq
- Invalid Cast Exception while using LINQ
- Linq test for null or empty fields
- Entity Framework 4: Eager Loading (Include) with filters using Self Tracking Entities
- DataGridView delete row when DataSource is List<myClass>