English 中文(简体)
LINQ VB.Net 变量未声明错误
原标题:LINQ VB.Net variable not declared error
  • 时间:2012-05-24 22:40:47
  •  标签:
  • vb.net
  • linq

我试图运行这行代码:

Dim OrderedFiles() As String = Directory.GetFiles(FilePath).OrderBy(x >= x.CreationTime)

在 x 上我有一个错误, 说 x 没有声明 。

我的项目设置为“选择严格关闭 ” 和“选择推断 ” 。 如果我选择了“选择严格 ”, 那么我就会从项目中发现数千个错误(这是继承的), 我没有时间来修正所有错误, 但是x不再给我一个错误。 直到我想把电脑扔出窗外之前,我一直在谷歌搜索 。

如能帮助纠正这一说法,将不胜感激。

编辑 :

我本来希望有一个更优雅的解决方案, 但这就是我为了解决这个特殊的问题而提出的。

    Dim fileList() As String = Directory.GetFiles(FilePath)
    Dim fileDate(fileList.Length - 1) As DateTime

    For i As Integer = 0 To fileList.Length - 1
        fileDate(i) = New FileInfo(fileList(i)).CreationTime
    Next

    Array.Sort(fileDate, fileList)

    With EmailTemplates_DropDownList
        .DataSource = fileList.Reverse.Take(5)
        .DataBind()
    End With

它并不特别优雅,但它能完成这项工作。我本来希望有一个LINQ线条解决方案,而我只是没有LINQ的背景来知道如何做这份工作,有时间去买一本书。

最佳回答
问题回答

Directory.GetFiles() returns the names of files (including their paths) in the specified directory. It s not possible to use x.CreationTime, x is a string

您也许应该使用目录Info

Dim di as DirectoryInfo = new DirectoryInfo(FilePath)
Dim OrderedFiles = di.GetFiles().OrderBy(Function(x) x.CreationTime).Take(3)
Dim fi as FileInfo
For each fi in OrderedFiles
    Console.WriteLine(fi.FullName)
Next




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签