please see my attached file for info...

please help...

bluehangook629 commented: not clear -1

Recommended Answers

All 2 Replies

Be more clear.
Explain what you are trying to achieve and you have explain little better than 'select the names in table2 where the point is less than the name's point in table2'

OK. Here's round 2. It includes the solution I stated in your other post as well as a different non-LINQ solution:

Imports System.Linq
Imports System.Collections.Generic

Module Module1
   Public Class CTable
      Public name As String
      Public points As Integer

      Public Sub New()
         name = ""
         points = 0
      End Sub

      Public Sub New(ByVal name As String, ByVal points As Integer)
         Me.name = name
         Me.points = points
      End Sub
   End Class

   Sub Main()

      Dim lstTable1 As New List(Of CTable) From
      {
         New CTable("abc", 10011),
         New CTable("def", 10022),
         New CTable("ghi", 10033)
      }

      Dim lstTable2 As New List(Of CTable) From
      {
         New CTable("jkl", 10000),
         New CTable("mno", 15550),
         New CTable("pqr", 10029)
      }

      ' ************************************************************************
      ' This method uses LINQ to produce a list of strings with the results
      Dim lst_strResult As List(Of String) = GetResultsWithLinq(lstTable1, lstTable2)
      lst_strResult.ForEach(Sub(s) Console.WriteLine(s))

      Console.WriteLine(vbCrLf & "-----------------------" & vbCrLf)

      ' ************************************************************************
      ' This method uses "For Each" to produce a list of strings with results
      lst_strResult = GetResultsWithoutLinq(lstTable1, lstTable2)
      lst_strResult.ForEach(Sub(s) Console.WriteLine(s))

   End Sub
   Function GetResultsWithoutLinq(ByVal lstTable1 As List(Of CTable), ByVal lstTable2 As List(Of CTable)) As List(Of String)
      Dim lst_strResult As New List(Of String)
      Dim map As New Dictionary(Of String, List(Of String))

      For Each rowA As CTable In lstTable1
         For Each rowB As CTable In lstTable2
            If rowA.points < rowB.points Then
               Continue For
            End If
            '
            If Not (map.ContainsKey(rowA.name)) Then
               map.Add(rowA.name, New List(Of String))
            End If
            '
            map(rowA.name).Add(rowB.name)
         Next
      Next

      For Each kvp As KeyValuePair(Of String, List(Of String)) In map
         lst_strResult.Add(kvp.Key & " is greater than " & String.Join(" and ", kvp.Value.ToArray()))
      Next

      Return lst_strResult
   End Function

   Function GetResultsWithLinq(ByVal lstTable1 As List(Of CTable), ByVal lstTable2 As List(Of CTable)) As List(Of String)
      Dim lst_strResult As List(Of String) =
      (
         From s In
         (
            From rowA In lstTable1
            From rowB In lstTable2
            Where rowB.points < rowA.points
            Select New With {.key = rowA.name, .value = rowB.name}
         ).ToLookup(Function(k) k.key, Function(v) v.value)
         Select (s.Key & " is greater than " & String.Join(" and ", s.ToArray()))
      ).ToList()

      Return lst_strResult
   End Function
End Module
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.