i have problem LINQ query.In above,i got error system.object cant be converted to Sytem.String. What can be the problem?
if i use string() instead of ArrayList, it doesn't raise error. But in String(), i should add items man开发者_Python百科ually
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
As a general rule, do not ever use ArrayList
or any of the other types in System.Collections
. These types are deprecated in favour of their generic equivalents (if available) in the namespace System.Collections.Generic
. The equivalent of ArrayList
happens to be List(Of T)
.
Secondly, returning an array from a method is generally considered bad practice – although even methods from the framework do this (but this is now widely considered a mistake). Instead, return either IEnumerable(Of T)
or IList(Of T)
, that is: use an interface instead of a concrete type.
You can use List(Of String)
instead of ArrayList.
Add a reference to System.Data.DataSetExtensions
and do:
return dt
.AsEnumerable() // sic!
.Select(r => r.Item("DS_TAG")) // DataTable becomes IEnumrable<DataRow>
.Where(m => m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
.ToArray();
(sorry but that's C# syntax, rewrite to VB.NET as you need)
精彩评论