views:

40

answers:

3

Hi,

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 manually

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
+1  A: 

You can use List(Of String) instead of ArrayList.

Mark Cidade
thanks,i tried it after posted here :) it works.thanks
ceee
A: 

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)

abatishchev
+1  A: 

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.

Konrad Rudolph