Answered by:
Combine two list(of T) collections to contain distict objects

Question
-
User1909155429 posted
Hi,
I have the following list collection that i want to combine with another List(of Items) collection
Dim ilistValues As IList(Of Product)
The List(of items) should first be filtered to contain only objects that have a property Name="First"
then joined with ilistValues collection and filtered to contain Product with DISTICT 'Email' property.
Wednesday, December 11, 2013 7:31 PM
Answers
-
User281315223 posted
If you have two seperate collections (of the same kind of items), you can use the AddRange() method available through LINQ to merge two collections into one. In your case, you could also use LINQ to apply the necessary filtering (through the Distinct() method or otherwise) and then merge your collections :
'Your first collection (you can also apply filters here through a Where clause)' Dim a = YourCollectionOfItems.Distinct().ToList() 'Your second collection' Dim b = YourOtherCollectionOfItems.Distinct().ToList() 'Your merged collection Dim c = a.AddRange(b)
So your case might look something like this :
'Your first collection (you can also apply filters here through a Where clause)' Dim firstNames = YourFirstCollection.Where(Function (f) f.FirstName = "First").ToList()
However to use a distinct on a specific property, you might want to consider using a third-party library like MoreLINQ which has a DistinctBy() method that may prove useful in a situation like this and it would allow you to get all of the objects with distinct email address values by using the following syntax :
Dim emailAddresses = YourCollection.DistinctBy(Function(f) f.Email).ToList()
so you would combine both of these into :
Dim firstNames = YourCollection.Where(Function (f) f.FirstName = "First").ToList() Dim emailAddresses = YourCollection.DistinctBy(Function (f) f.Email).ToList() 'Build your final collection by storing both results in a single collection' Dim collection = firstNames.AddRange(emailAddresses)
If you wanted to perform an actual JOIN, you would likely want to the available Join() method through LINQ.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 13, 2013 2:56 PM -
User-1016547394 posted
Imports System Imports System.Collections.Generic Class Example Public Shared Sub Main() Try Dim boxEqC As New BoxEqualityComparer() Dim boxes As Dictionary(Of Box, [String]) = _ New Dictionary(Of Box, String)(boxEqC) Dim redBox As New Box(4, 3, 4) Dim blueBox As New Box(4, 3, 4) boxes.Add(redBox, "red") boxes.Add(blueBox, "blue") Catch argEx As ArgumentException Console.WriteLine(argEx.Message) End Try End Sub End Class Public Class Box Public Sub New(ByVal h As Integer, ByVal l As Integer, _ ByVal w As Integer) Me.Height = h Me.Length = l Me.Width = w End Sub Private _Height As Integer Public Property Height() As Integer Get Return _Height End Get Set(ByVal value As Integer) _Height = value End Set End Property Private _Length As Integer Public Property Length() As Integer Get Return _Length End Get Set(ByVal value As Integer) _Length = value End Set End Property Private _Width As Integer Public Property Width() As Integer Get Return _Width End Get Set(ByVal value As Integer) _Width = value End Set End Property End Class Class BoxEqualityComparer Implements IEqualityComparer(Of Box) Public Overloads Function Equals(ByVal b1 As Box, ByVal b2 As Box) _ As Boolean Implements IEqualityComparer(Of Box).Equals If b1.Height = b2.Height And b1.Length = _ b2.Length And b1.Width = b2.Width Then Return True Else Return False End If End Function Public Overloads Function GetHashCode(ByVal bx As Box) _ As Integer Implements IEqualityComparer(Of Box).GetHashCode Dim hCode As Integer = bx.Height Xor bx.Length Xor bx.Width Return hCode.GetHashCode() End Function End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2013 3:22 AM
All replies
-
User197322208 posted
var q = iListValues.Where(item=>item.Name=="First").ToList()
See also the Distinct function
then use addrange.
Wednesday, December 11, 2013 8:53 PM -
User1909155429 posted
Is that related to a Custom object ?
Because it does not work with my ILIST(OF PRODUCT)
Also, is it possible to join two List collections together through a Query and amalgamate into one ?
Thanks
Friday, December 13, 2013 2:34 PM -
User281315223 posted
If you have two seperate collections (of the same kind of items), you can use the AddRange() method available through LINQ to merge two collections into one. In your case, you could also use LINQ to apply the necessary filtering (through the Distinct() method or otherwise) and then merge your collections :
'Your first collection (you can also apply filters here through a Where clause)' Dim a = YourCollectionOfItems.Distinct().ToList() 'Your second collection' Dim b = YourOtherCollectionOfItems.Distinct().ToList() 'Your merged collection Dim c = a.AddRange(b)
So your case might look something like this :
'Your first collection (you can also apply filters here through a Where clause)' Dim firstNames = YourFirstCollection.Where(Function (f) f.FirstName = "First").ToList()
However to use a distinct on a specific property, you might want to consider using a third-party library like MoreLINQ which has a DistinctBy() method that may prove useful in a situation like this and it would allow you to get all of the objects with distinct email address values by using the following syntax :
Dim emailAddresses = YourCollection.DistinctBy(Function(f) f.Email).ToList()
so you would combine both of these into :
Dim firstNames = YourCollection.Where(Function (f) f.FirstName = "First").ToList() Dim emailAddresses = YourCollection.DistinctBy(Function (f) f.Email).ToList() 'Build your final collection by storing both results in a single collection' Dim collection = firstNames.AddRange(emailAddresses)
If you wanted to perform an actual JOIN, you would likely want to the available Join() method through LINQ.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 13, 2013 2:56 PM -
User-1016547394 posted
Imports System Imports System.Collections.Generic Class Example Public Shared Sub Main() Try Dim boxEqC As New BoxEqualityComparer() Dim boxes As Dictionary(Of Box, [String]) = _ New Dictionary(Of Box, String)(boxEqC) Dim redBox As New Box(4, 3, 4) Dim blueBox As New Box(4, 3, 4) boxes.Add(redBox, "red") boxes.Add(blueBox, "blue") Catch argEx As ArgumentException Console.WriteLine(argEx.Message) End Try End Sub End Class Public Class Box Public Sub New(ByVal h As Integer, ByVal l As Integer, _ ByVal w As Integer) Me.Height = h Me.Length = l Me.Width = w End Sub Private _Height As Integer Public Property Height() As Integer Get Return _Height End Get Set(ByVal value As Integer) _Height = value End Set End Property Private _Length As Integer Public Property Length() As Integer Get Return _Length End Get Set(ByVal value As Integer) _Length = value End Set End Property Private _Width As Integer Public Property Width() As Integer Get Return _Width End Get Set(ByVal value As Integer) _Width = value End Set End Property End Class Class BoxEqualityComparer Implements IEqualityComparer(Of Box) Public Overloads Function Equals(ByVal b1 As Box, ByVal b2 As Box) _ As Boolean Implements IEqualityComparer(Of Box).Equals If b1.Height = b2.Height And b1.Length = _ b2.Length And b1.Width = b2.Width Then Return True Else Return False End If End Function Public Overloads Function GetHashCode(ByVal bx As Box) _ As Integer Implements IEqualityComparer(Of Box).GetHashCode Dim hCode As Integer = bx.Height Xor bx.Length Xor bx.Width Return hCode.GetHashCode() End Function End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2013 3:22 AM -
User1909155429 posted
pr = From p In product.Values Group p.Price By Key = p.Email Into Group = Group _ Select New With {.Email = Key, .Price = Group.Sum()}
i have been working with a linq query like this.
i have another custom list collection list(of product)
i want to find the product that match on email property without enumerating pr ?
i use linq here because it is convenient for sum the Price into groups (Email)
Sunday, December 29, 2013 4:28 PM -
User1909155429 posted
is this the method used for comparing custom type objects ?
and not those given previous ?
Thanks
Tuesday, December 31, 2013 3:24 PM