0

So I have two lists containing blood tests.

The first list contains tests that are offered and the second list contains tests that have previously been ordered. List 1 can have 5 tests only and list 2 can have hundreds of entries that I have retrieved from a text file.

I want to compare the lists and see how many of the tests in list 1 have been repeated on list 2 and also return how many times they were repeated.

for eg: I would like to return that test1 was repeated 5 times and test 2 repeated 3 times etc.. for the sake of statistics really.

Dim List1 As New List(Of String)()
Dim List2 As New List(Of String)()

List1.Add(test1)             
List1.Add(test2)             
List1.Add(test3)
List1.Add(test4)
List1.Add(test5)

List2.Add(test1)
List2.Add(test1)
List2.Add(test5)
List2.Add(test4)
List2.Add(test1)
List2.Add(test2)
List2.Add(test2)
List2.Add(test1)
List2.Add(test2)
List2.Add(test1)
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
Yawallek
  • 27
  • 4
  • It sounds like the first list is basically irrelevant and you just want to know what items there are in the second list and how many times they appear. The first list would only be relevant if you wanted to get zero for each item that doesn't appear in the second list, but your question doesn't indicate that that is the case. There are various ways this could be done but the obvious one is with a loop and just put the items into groups and count the items in each group at the end. Surely you can at least try something like that for yourself first. – John May 05 '22 at 16:03
  • Does this answer your question? [How to Count Duplicates in List with LINQ](https://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq) – Étienne Laneville May 05 '22 at 16:41
  • You're right, just over-complicating things with the first list. – Yawallek May 05 '22 at 18:31

1 Answers1

2

This might work,

    Dim List2 As New List(Of String)
    List2.Add("test1")
    List2.Add("test1")
    List2.Add("test5")
    List2.Add("test4")
    List2.Add("test1")
    List2.Add("test2")
    List2.Add("test2")
    List2.Add("test1")
    List2.Add("test2")
    List2.Add("test1")

    Dim groupedNames As IEnumerable(Of IGrouping(Of String, String))
    groupedNames = List2.GroupBy(Function(x) x)
    If groupedNames IsNot Nothing AndAlso groupedNames.Count > 0 Then
        For Each t As IEnumerable(Of String) In groupedNames
            Debug.Print(t(0) & " - " & t.Count.ToString)
        Next
    End If
dbasnett
  • 11,334
  • 2
  • 25
  • 33