I have two instances of IEnumerable<T> (with the Different T). I want to combine both of them .
IEnumerable<ClassA>
IEnumerable<ClassB>
Both in CLass A and ClassB i have one common property .Lets say for example it is EmpId ..
Is there a build-in method in .Net to do that or do I have to write it myself?
Assuming you can extract the common property to a common interface, let's say IEmployee, then you could just Cast() and then Concatenate the collections:
classAItems.Cast<IEmployee>().Concat(classBItems)
Note that this will only iterate over those IEnumerables on demand. If you want to create a List containing the content of both sequences at the time you combined them, you can use ToList():
List<IEmployee> all = classAItems.Cast<IEmployee>().Concat(classBItems).ToList();
You can do the same if you only need an array using ToArray().
You can get the concatenated common property easily enough:
var empIds = first.Select(x => x.EmpId).Concat(second.Select(x => x.EmpId));
If this is not what you are after, you will have to be more specific.
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