When I try to use the
Dim warningList As List(Of Warning)
Dim item = warningList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
if(type = "Warning") Then 'Please note that this condition is true...
itemList = warningList
Dim item = itemList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
Now that you've edited your question it's clear.
itemList without a type, so it's
Object implicitly(in VB.NET with option strict set to
Off which i strongly recommend against).
Now that you have declared a variable of type Object you can asssign any type to it. But you would have to cast it back to its real type
List(Of Warning) to be able to use list or LINQ methods(which extend
But instead declare it with the correct type:
Dim itemList As List(Of Warning) if(type = "Warning") Then itemList = warningList End If Dim item = itemList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
Including to comment to explain why
Warning is not related to this problem:
That's not the real code. If
warningListis really a
List(Of Warning)you should be able to use
Enumerable.Where(if LINQ is imported). The fact that you assign this instance to another variable (on declaration) doesn't change anything because that variable's type is also a
List(Of Warning). So
itemList.Whereshould work too.
Warninghas nothing to do with it because the type which is extended by
Tcan be any type(even
IEnumerable(Of T)you can use
Enumerable.Whereon any list (or array).
If you actually have multiple types and
Warning is just one of it, you should implement a common interface. Here's an example:
Public Enum NotificationType Warning Info [Error] End Enum Public Interface INamedNotification ReadOnly Property Type As NotificationType Property Name As string End Interface Public Class Warning Implements INamedNotification Public Sub New( name As String ) Me.Name = name End Sub Public Property Name As String Implements INamedNotification.Name Public ReadOnly Property Type As NotificationType Implements INamedNotification.Type Get Return NotificationType.Warning End Get End Property End Class
Now you can declare a
List(Of INamedNotification) and fill it with whatever implements this interface, like the
Dim notificationList As List(Of INamedNotification) if type = "Warning" Then itemList = warningList Else If type = "Info" itemList = infoList End If Dim item = notificationList.Where(Function(x) x.Name = "Foo").FirstOrDefault()