I have an object model that contains a list of longs. I want to get the combined list of longs of two different instances. When I write this:
var MyCombinedList = TheObject1.ListOfLongs.Union(TheObject2.ListOfLongs);
I get an empty list. Note that sometimes TheObject2
can have an empty list.
What am I doing wrong?
Thanks.
Union is an extension method to merge two collections. It requires at least two collections to perform the merge operation, but that merged collection holds only the distinct elements from both the collections.
Linq, acts upon 2 collections. It returns a new collection that contains the elements that are found. Union removes duplicates.
The following table lists all Set operators available in LINQ. Returns distinct values from a collection. Returns the difference between two sequences, which means the elements of one collection that do not appear in the second collection.
Use Concat() this will concatenates two sequences. So try this instead :
var MyCombinedList = TheObject1.ListOfLongs.Concat(TheObject2.ListOfLongs);
Good Luck !!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With