find complete object in list of objects - vb.net

is it possible to find a complete object in list of objects?
dim list1 as list(of class1) = alist
dim x as class1
how to find x in list1 if it exists without comparing a single property like ID ?

The Contains method will return true if the element is in the list.
dim list1 as list(of class1) = alist
dim x as class1
list1.Contains(x)

Related

Create a dictionary from a list of lists VB.Net

I have a list of objects within another list. It looks like this:
Dim _elements as List(Of List(Of Element))
_elements(0) is a list of Element1, Element2, etc.
_elements(1) is a list of Element5, Element6, etc.
I need to create a dictionary of the elements so that the index of each list is the Key in the dictionary. The value of the dictionary should hold all elements of the Legend type that can be found in the list. Something like the following:
Dim legendElements As New Dictionary(Of Integer, List(Of Element))
' Initialize the list in the dictionary
For i As Integer = 0 To _elements.Count - 1
legendElements(i) = new List(Of Element)
Next
For i As Integer = 0 To _elements.Count - 1
For each element as Element in _elements(i)
If element.GetType() Is GetType(Legend) Then
legendElements.add(element)
End If
Next
Next
Is there a System.Linq function that can do the equivalent as the example above?
Call Enumerable.SelectMany method to get a flatten list of the target type which you filter by calling the Enumerable.Where method. Call the Enumerable.Select method to create from the result IEnumerable of anonymous objects to hold and pass the Legend objects along with their indices, and finally, call the Enumerable.ToDictionary method to create
Dictionary(Of Integer, Element) from the anonymous objects.
Dim legendElements As Dictionary(Of Integer, Element) = elements.
SelectMany(Function(x) x.Where(Function(y) y.GetType Is GetType(Legend))).
Select(Function(v, k) New With {Key .Key = k, .Value = v}).
ToDictionary(Function(a) a.Key, Function(a) a.Value)
Do the proper cast if you need a Dictionary(Of Integer, Legend).
Dim legendElements As Dictionary(Of Integer, Legend) = elements.
SelectMany(Function(x) x.Where(Function(y) y.GetType Is GetType(Legend))).
Select(Function(v, k) New With {Key .Key = k, .Value = v}).
ToDictionary(Function(a) a.Key, Function(a) DirectCast(a.Value, Legend))

VB NET: how to find a list of string which has max item count, from several list of string

Could you please help me for this:
I have 5 lists of string, lets say A, B, C, D, and E:
A has 6 items
B has 5 items
C has 9 items
D has 2 items
E has 7 items
I need to sort or to find, "C" as the list which has max items.
I need to create tab in winform and on every tab I need to create datagridview programmatically. the maximum count in the list will be the maximum tab I need to create. And on every tab, there will be 1 item of every list member. Of course not all tab will have item from member which has small item count.
Previously what Ii did is iterate through table and datagrid to construct and solve the problem to avoid sorting the list cause I have no idea to find the max items on those lists.
UPDATE: Helped by Andrew
` Dim z As New List(Of List(Of String))
Dim a As New List(Of String)
a.Add("a1")
a.Add("a2")
a.Add("a3")
Dim b As New List(Of String)
b.Add("b1")
b.Add("b2")
b.Add("b3")
b.Add("b4")
b.Add("b5")
Dim c As New List(Of String)
c.Add("c1")
c.Add("c2")
c.Add("c3")
c.Add("c3")
z.Add(a)
z.Add(b)
z.Add(c)
Dim maxItems = z.Max(Function(p) p.Count)
MessageBox.Show(maxItems)`
If all you need is the length of the longest list...
Private A As New List(Of String) From {"Mathew", "Mark", "Luke", "John"}
Private B As New List(Of String) From {"Apples", "Oranges", "Pears"}
Private C As New List(Of String) From {"Haddock", "Salmon"}
Private D As New List(Of String) From {"Great Dane", "Poodle", "Bulldog", "Spaniel", "Golden Retriever"}
Private Sub GetMaxListLength()
Dim E() As Integer = {A.Count, B.Count, C.Count, D.Count}
Dim max = E.Max
MessageBox.Show(max.ToString)
End Sub
Both Mary answer and Andrew's work perfectly:
Dim z As New List(Of List(Of String))
Dim a As New List(Of String)
a.Add("a1")
a.Add("a2")
a.Add("a3")
Dim b As New List(Of String)
b.Add("b1")
b.Add("b2")
b.Add("b3")
b.Add("b4")
b.Add("b5")
Dim c As New List(Of String)
c.Add("c1")
c.Add("c2")
c.Add("c3")
c.Add("c3")
z.Add(a)
z.Add(b)
z.Add(c)
Dim eb() As Integer = {a.Count, b.Count, c.Count}
Dim max = eb.Max
Dim maxItems = z.Max(Function(p) p.Count)
MessageBox.Show(max)

how to check that a list contains another list item in VB.Net

I have two lists like this.
Dim List1 As IList(Of String) = New List(Of String)
Dim ListMatch As IList(Of String) = New List(Of String)
I need to figure out if List1 contains all items of ListMatch. How can i do this is VB.Net?
You can use Not SmallCollection.Except(LargeCollection).Any:
Dim containsAll = Not ListMatch.Except(List1).Any()
Documentation of Enumerable.Except:
This method returns those elements in first that do not appear in
second. It does not also return those elements in second that do not
appear in first.
Since Except is a set method, it doesn't take duplicates into account. So if you also want to know if List1 contains the same count of items of ListMatch you could use(less efficient):
Dim list1Lookup = List1.ToLookup(Function(str) str)
Dim listMatchLookup = ListMatch.ToLookup(Function(str) str)
Dim containsSameCount = listMatchLookup.
All(Function(x) list1Lookup(x.Key).Count() = x.Count())

Defining the type of a List in vb.net at runtime

I'm creating a class at run time using typebuilder and after I create this class I want to define its type for a list like
dim fooList as new List(of DynamicClassName)
Since this doesn't exist at compile time of course it throws an error. When I generate this type I return the type so I can't do something like
dim newType = createNewType(foobar)
dim fooList as new List(of getType(newType))
How do I assign the type of a List at runtime?
You can create a List(Of T), but AFAIK you won't be able to cast it to a typed object. I've used the String type in the following example.
Dim list As Object = Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {GetType(String)}))
Debug.WriteLine((TypeOf list Is List(Of String)).ToString())
Output
True
So in your case it would look like this:
Dim newType = createNewType(foobar)
'Creates a List(Of foobar):
Dim list As IList = Ctype(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {newType})), IList)
'Creates a BindingList(Of foobar):
Dim bindingList As IBindingList = Ctype(Activator.CreateInstance(GetType(BindingList(Of )).MakeGenericType(New Type() {newType})), IBindingList)
This does not answer your question, but may solve your problem.
Another option would be to use an ArrayList (for which you don't have to assign a type). You can see the details here: http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx.
Here is a basic example:
Dim anyArrayList as new ArrayList()
anyArrayList.add("Hello")
anyArrayList.add("Testing")

.NET: Quick way of using a List(Of String) as the Values for a new Key/Value collection?

I have a List(Of String) object and need to create a new collection based on the strings' values. The new collection will be of a custom class with two string fields - call them Key and Value (but it's not the built-in KeyValue class, it's a custom class).
All the values of Key will be the same, it's just Value that I want to source from the string list. Example:
Dim slValues = New List(Of String)({"Cod", "Halibut", "Herring"})
Dim myList = New List(Of myClass)( ... amazing initialisation line here? )
Class myClass
Public Key As String ' This will always be "Fish"
Public Value As String ' This will be the fish name.
End Class
(Note I don't actually have access to the class I'm using, so can't just change it to Public Key As String = "Fish" as a default. Key must be set at runtime.)
I can of course just do a loop and do it manually, but I'm wondering if there's a whiz-bang way to achieve this as part of the initialisation line?
How about this
Dim myList = slValues.ConvertAll(Of myClass)( _
Function(s) New YourClass With {.Key = "Fish", .Value = s})
Here's an alternative to Jodrell's answer using Linq's Select function:
Dim myList = slValues.Select(Function(fish) New CustomClass With {.Key = "Fish", .Value=fish}).ToList()
from x in Enumerable.Range(1, slValues.count-1).ToArray
select new KeyValuePair(of string,myClass) with {.Key=slValues(x),.Value=myList(x)}
hope it helps.