I have been trying to dive into more advanced features of C# like LINQ and Lambda expressions, so I am a complete beginner when it comes to LINQ ans Lambda expressions.
My problem is that I have a list of paths of files contained on my computer and want to sort them based on the "last access time". To do this I wrote the following
我一直试图深入研究C#的更高级功能,如LINQ和Lambda表达式,所以当涉及到LINQ和Lambda表达式时,我是一个完全的初学者。我的问题是我有一个包含在我的计算机上的文件路径列表,并希望根据“上次访问时间”对它们进行排序。为此,我写了以下内容
TempList = FilesList.OrderByDescending((FileInfo Files,string n) => { Files = new FileInfo(n) ;
Files.LastAccessTime ; } ) ;
FilesList
contains the paths of files in various orders. FilesList
is of type list<string>
and TempList
is of type IEnumerable<string>
.
FilesList包含各种顺序的文件路径。 FilesList的类型为list
To this query compiler is generating the following error:
对此查询编译器生成以下错误:
The type arguments for method 'System.Linq.Enumerable.OrderByDescending<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
无法从用法中推断出方法'System.Linq.Enumerable.OrderByDescending
Can someone please point out what is wrong with my query. I am unable to understand from the error message.
有人可以指出我的查询有什么问题。我无法从错误消息中理解。
2 个解决方案
#1
1
First of all, there is no way to get FileInfo
parameter when FilesList
is List<string>
. You just get string. Furthermore, you don't return anything from your lambda. And you can't reassign lambda parameters either.
首先,当FilesList为List
TempList = FilesList.OrderByDescending(n => new FileInfo(n).LastAccessTime);
Or, if you want to use {}
within your lambda to make it multi-line lambda, you have to return
something somewhere in the code (most likely as the last statement):
或者,如果你想在你的lambda中使用{}来使它成为多行lambda,你必须在代码中的某处返回一些东西(最有可能是最后一个语句):
TempList = FilesList.OrderByDescending(n => { var file = new FileInfo(n); return file.LastAccessTime });
#2
2
Try this instead (assuming your list of "files" is actually a list of fully-qualified file names):
试试这个(假设你的“文件”列表实际上是一个完全限定的文件名列表):
TempList = FilesList.OrderByDescending(n => new FileInfo(n).LastAccessTime).ToList();
I can't promise the syntax is completely correct as I don't have a list of files to run it on. Also, there may be more efficient methods than instantiating a FileInfo
object for each file.
我不能保证语法是完全正确的,因为我没有要运行它的文件列表。此外,可能有比为每个文件实例化FileInfo对象更有效的方法。
#1
1
First of all, there is no way to get FileInfo
parameter when FilesList
is List<string>
. You just get string. Furthermore, you don't return anything from your lambda. And you can't reassign lambda parameters either.
首先,当FilesList为List
TempList = FilesList.OrderByDescending(n => new FileInfo(n).LastAccessTime);
Or, if you want to use {}
within your lambda to make it multi-line lambda, you have to return
something somewhere in the code (most likely as the last statement):
或者,如果你想在你的lambda中使用{}来使它成为多行lambda,你必须在代码中的某处返回一些东西(最有可能是最后一个语句):
TempList = FilesList.OrderByDescending(n => { var file = new FileInfo(n); return file.LastAccessTime });
#2
2
Try this instead (assuming your list of "files" is actually a list of fully-qualified file names):
试试这个(假设你的“文件”列表实际上是一个完全限定的文件名列表):
TempList = FilesList.OrderByDescending(n => new FileInfo(n).LastAccessTime).ToList();
I can't promise the syntax is completely correct as I don't have a list of files to run it on. Also, there may be more efficient methods than instantiating a FileInfo
object for each file.
我不能保证语法是完全正确的,因为我没有要运行它的文件列表。此外,可能有比为每个文件实例化FileInfo对象更有效的方法。