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.
Member
288 Points
886 Posts
Combine two list(of T) collections to contain distict objects
Dec 11, 2013 07:31 PM|peterthegreat|LINK
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.
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: Combine two list(of T) collections to contain distict objects
Dec 11, 2013 08:53 PM|ignatandrei|LINK
var q = iListValues.Where(item=>item.Name=="First").ToList()
See also the Distinct function
then use addrange.
Member
288 Points
886 Posts
Re: Combine two list(of T) collections to contain distict objects
Dec 13, 2013 02:34 PM|peterthegreat|LINK
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
All-Star
114593 Points
18503 Posts
MVP
Re: Combine two list(of T) collections to contain distict objects
Dec 13, 2013 02:56 PM|Rion Williams|LINK
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 :
So your case might look something like this :
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 :
so you would combine both of these into :
If you wanted to perform an actual JOIN, you would likely want to the available Join() method through LINQ.
Member
50 Points
31 Posts
Re: Combine two list(of T) collections to contain distict objects
Dec 17, 2013 03:22 AM|Bosen Tom|LINK
Member
288 Points
886 Posts
Re: Combine two list(of T) collections to contain distict objects
Dec 29, 2013 04:28 PM|peterthegreat|LINK
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)
Member
288 Points
886 Posts
Re: Combine two list(of T) collections to contain distict objects
Dec 31, 2013 03:24 PM|peterthegreat|LINK
is this the method used for comparing custom type objects ?
and not those given previous ?
Thanks