ArrayList Problem in LINQ - vb.net

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

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)

Related

Display the list of the Selected items in the checkboxlist

Dim list As New List(Of String)
list = chkparameter.Items
.Cast(Of ListItem)
.AsEnumerable()
.Where(Function(x) x.Selected)
.Select(Function(x) x.Value)
The error i am getting is
Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Web.UI.WebControls.ListItem,System.String]' to type 'System.Collections.Generic.List1[System.String]'.
How can i rectify it.
Thanks
If you want to assign the result to a List(Of String) variable then you need a List(Of String) object. You can all ToList on any enumerable list to create a List(Of T).
Also, your AsEnumerable call is pointless because Cast(Of T) already returns an IEnumerable(Of T).
Finally, declaring a variable on one line and then setting its value is so unnecessarily verbose. It's not wrong but it is pointless. In your case, not only are you declaring a variable but you're also creating an object that you never use. Don't create a New object if you don;t actually want a new object, which you don;t because you're getting an object on the very next line.
Dim list As List(Of String) = chkparameter.Items.
Cast(Of ListItem).
Where(Function(x) x.Selected).
Select(Function(x) x.Value).
ToList()
There's also no need to declare the type of the variable because it will be inferred from the initialising expression, i.e. ToList returns a List(Of String) so the type of the variable can be inferred from that. Not everyone likes to use type inference where it's not completely obvious though, so I'll let you off that one. I'd tend to do this though:
Dim list = chkparameter.Items.
Cast(Of ListItem).
Where(Function(x) x.Selected).
Select(Function(x) x.Value).
ToList()
By the way, notice how much easier the code is to read with some sensible formatting? If you're going to use chained function syntax like that, it's a very good idea to put each function on a different line once you get more than two or three.

Type of Generic seems worthless

Please see the code below:
Public Function ExecuteDynamicQuery(Of T As New)(ByVal sql As String, ByVal type As T) As List(Of T) Implements IGenie.ExecuteDynamicQuery
Dim iConnectionBLL As iConnectionBLL = New clsConnectionBLL
Dim paramValues() As DbParameter = New clsParameterValues().getParameterValues()
Using conn As DbConnection = iConnectionBLL.getDatabaseTypeByDescription("Genie2"), _
rdr As DbDataReader = clsDatabaseHelper.ExecuteReader(conn, CommandType.Text, sql, paramValues)
Dim list As List(Of T) = New List(Of T)
While rdr.Read()
Dim hello As New T
Dim method As MethodInfo = GetType(clsType).GetMethod("PopulateDataReader")
method.Invoke(hello, New Object() {rdr})
list.Add(hello)
End While
Return list
End Using
End Function
Is there a way of executing the SQL statement above without passing in type as an arguement. It seems a bit pointless - the only reason it is there is to let the function know the type of the generic.
Well you can change the method to not have the second parameter:
Public Function ExecuteDynamicQuery(Of T As New)(ByVal sql As String) As List(Of T) Implements IGenie.ExecuteDynamicQuery
However:
You'd need to change IGenie as well
The caller would then need to explicitly specify the type argument, instead of letting the compiler infer it on the basis of the argument (which would no longer be present)

Casting Error when sorting with IComparer

I have an ArrayList of strings of the form "Q19_1_1", "Q19_10_1", "Q19_5_1".
With the normal sort method the list will be sorted as
"Q19_1_1"
"Q19_10_1"
"Q19_5_1"
But I would like to sort it numerically based off the second integer in name and then the third. So I would like:
"Q19_1_1"
"Q19_5_1"
"Q19_10_1"
My Sub:
Dim varSet As New ArrayList
varSet.Add("Q19_1_1")
varSet.Add("Q19_10_1")
varSet.Add("Q19_5_1")
varSet.Sort(New VariableComparer())
I have a IComparer:
Public Class VariableComparer
Implements IComparer(Of String)
Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
Dim varPartsX As Array
Dim varPartsY As Array
varPartsX = x.Split("_")
varPartsY = y.Split("_")
Return String.Compare(varPartsX(1), varPartsY(1))
End Function
End Class
But when I attempt to sort I get the error:
Unable to cast object of type 'VBX.VariableComparer' to type 'System.Collections.IComparer'.
VariableComparer implements IComparer but I'm guessing it can't be of type IComparer(Of String)?
How can I resolve this issue? What am I missing?
Using a List(Of String) also gives you access to the LINQ extensions. Specifically the OrderBy and the ThenBy extensions. You could do it something like this:
Dim test3 As New List(Of String)({"Q19_1_1", "Q19_10_1", "Q19_5_1", "Q19_5_2"})
test3 = test3.OrderBy(Of Integer)(Function(s) Integer.Parse(s.ToString.Split("_"c)(1))) _
.ThenBy(Of Integer)(Function(s2) Integer.Parse(s2.ToString.Split("_"c)(2))).ToList
Casting to Integer gives you the proper sorting without using a new IComparer interface
You are correct - the issue is that you implemented IComparer(Of String), but not IComparer, which is a completely different interface.
If you switch to use a List(Of String) instead of ArrayList, it will work correctly.
This will also give you type safety within your collection.
In general, ArrayList (and the other System.Collections types) should be avoided in new development.

Generic extension method, List(Of T)

I am simply trying to spit out a string which aggregates a List's values into a NewLine delimited string.
<Extension()>
Public Function ToColumn(Of T)(ByVal source As IEnumerable(Of T)) As String
' assuming Aggregate(Of String) will be inefficient, using StringBuilder
Dim sb As New StringBuilder()
For Each val As T In source
sb.Append(val.ToString() & Environment.NewLine)
Next
Return sb.ToString()
End Function
This method will help while debugging to see the entire contents of a List, while ?source only spits out the first 100 elements in the immediate window (maybe there is a different way to do this?). I just want to be able to copy it over to something like Excel quickly for analysis.
The problem is that although this method compiles, in usage, it is not recognized as an extension of List(of T) i.e.
Dim result As Int32()
Debug.Print(result.ToList().ToColumn())
' this line doesn't compile:
' 'toColumn' is not a member of 'System.Collections.Generic.List(Of Integer)'
I'd like to reuse this method on anything which I can box into an Object and get .ToString() (everything!), hence the generic aspect of it. I have seen many people add constraints to the generic T, i.e. Where T : Constraint, but my constraint would be Object, as is the nature of this method, which doesn't compile.
My question is why isn't it recognized as an extension of List(Of Integer). Also, is what I want to achieve possible?
Have you tried using an extension method on List(Of T) instead of IEnumerable(Of T), like this:
<Extension()>
Public Function ToColumn(Of T)(ByVal source As List(Of T)) As String
' assuming Aggregate(Of String) will be inefficient, using StringBuilder
Dim sb As New StringBuilder()
For Each val As T In source
sb.Append(val.ToString() & Environment.NewLine)
Next
Return sb.ToString()
End Function

VB.NET ArrayList to List(Of T) typed copy/conversion

I have a 3rd party method that returns an old-style ArrayList, and I want to convert it into a typed ArrayList(Of MyType).
Dim udc As ArrayList = ThirdPartyClass.GetValues()
Dim udcT AS List(Of MyType) = ??
I have made a simple loop, but there must be a better way:
Dim udcT As New List(Of MyType)
While udc.GetEnumerator.MoveNext
Dim e As MyType = DirectCast(udc.GetEnumerator.Current, MyType)
udcT.Add(e)
End While
Dim StronglyTypedList = OriginalArrayList.Cast(Of MyType)().ToList()
' requires `Imports System.Linq`
Duplicate.
Have a look at this SO-Thread: In .Net, how do you convert an ArrayList to a strongly typed generic list without using a foreach?
In VB.Net with Framework < 3.5:
Dim arrayOfMyType() As MyType = DirectCast(al.ToArray(GetType(MyType)), MyType())
Dim strongTypeList As New List(Of MyType)(arrayOfMyType)
What about this?
Public Class Utility
Public Shared Function ToTypedList(Of C As {ICollection(Of T), New}, T)(ByVal list As ArrayList) As C
Dim typedList As New C
For Each element As T In list
typedList.Add(element)
Next
Return typedList
End Function
End Class
If would work for any Collection object.
I would like to point out something about both the DirectCast and System.Linq.Cast (which are the same thing in the latest .NET at least.) These may not work if the object type in the array is defined by the user class, and is not easily convertable into object types that .NET recognizes. I do not know why this is the case, but it seems to be the problem in the software for which I am developing, and so for these we have been forced to use the inelegant loop solution.