【问题标题】:ArrayList Problem in LINQLINQ 中的 ArrayList 问题
【发布时间】:2010-08-08 12:33:45
【问题描述】:

我有问题 LINQ 查询。在上面,我得到错误 system.object 无法转换为 Sytem.String。可能是什么问题?

如果我使用 string() 而不是 ArrayList,它不会引发错误。但是在 String() 中,我应该手动添加项目

Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
     Dim movies As New ArrayList()
        Dim dt As DataTable = StaticData.Get_Data(StaticData.Tables.LU_TAG)
        For Each row As DataRow In dt.Rows
            movies.Add(row.Item("DS_TAG"))
        Next
     Return (From m In movies _
             Where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) _
             Select m).Take(count).ToArray()
End Function

【问题讨论】:

    标签: vb.net linq arraylist


    【解决方案1】:

    作为一般规则,永远不要使用ArrayListSystem.Collections 中的任何其他类型。这些类型已被弃用,取而代之的是它们在命名空间System.Collections.Generic 中的通用等价物(如果可用)。 ArrayList 的等价物恰好是 List(Of T)

    其次,从方法返回数组通常被认为是不好的做法——尽管框架中的方法也会这样做(但现在这被广泛认为是一个错误)。而是返回IEnumerable(Of T)IList(Of T),即:使用接口而不是具体类型。

    【讨论】:

      【解决方案2】:

      您可以使用 List(Of String) 代替 ArrayList。

      【讨论】:

        【解决方案3】:

        添加对System.Data.DataSetExtensions 的引用并执行以下操作:

        return dt
            .AsEnumerable() // sic!
            .Select(r => r.Item("DS_TAG")) // DataTable becomes IEnumrable<DataRow>
            .Where(m => m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
            .ToArray();
        

        (对不起,这是C#语法,根据需要重写为VB.NET)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-25
          • 2014-06-09
          • 2010-12-01
          • 1970-01-01
          • 2011-03-02
          相关资源
          最近更新 更多