List<int> lst1 = new List<int>{1,2,3,5,2};
List<int> lst2 = new List<int>{4,5,6,1,6};
List<int> lst3 = new List<int>();
Expected Output: lst3={1,2,3,4,5,6}
Can anyone help me with the LINQ code to select distinct elements from two lists ?
Thank you
Use the Union() method, which produces the Set Union of two lists, returning a new list containing all the items that exist in both lists:
lst3 = list1.Union(lst2).OrderBy(p=>p).ToList();
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