Can someone tell me what I’m doing wrong with the following Linq query? I’m trying to find the directory with the highest aphanumerical value.
DirectoryInfo[] diList = currentDirectory.GetDirectories();
var dirs = from eachDir in diList
orderby eachDir.FullName descending
select eachDir;
MessageBox.Show(dirs[0].FullName);
EDIT:
The above code does not compile, the error that the compiler generates is:
Cannot apply indexing with [] to an expression of type 'System.Linq.IOrderedEnumerable<System.IO.DirectoryInfo>
You’re trying to access
dirsas if it were an array or a list. It’s just anIEnumerable<T>. Try this:Note that I haven’t used a query expression here because it seems pretty pointless for just a single clause. You could put it all into one statement, too: