Using LINQ Concat to combine data
LINQ
26 Articles
In this article, let's learn about how to use Concat in LINQ in .NET.
Note: If you have not done so already, I recommend you read the article on Using LINQ Union to combine data.
Table of Contents
- Introduction
- Using LINQ Concat to combine primitive types
- Using LINQ Concat to combine with Equality Comparer
- Summary
Introduction
When working with two collections, we can combine them using LINQ Concat() method. This will combine two collections and gives a single collection with duplicates.
LINQ Concat() is used to answer questions about collection such as
- Combining multiple data set from different sources for analysis
- Git merge and combine files with changes from both commits
- Append lines to files
Using LINQ Concat to combine primitive types
Primitive data types like int, decimal, string, etc can just compare the values against other value in the collection
Code Sample - LINQ Concat Primitive Types
Using LINQ Concat to combine with Equality Comparer
So, combining primitive data types with Concat() is easy and straight forward. The same goes with objects. There is no need for comparer as Concat() is going to simply combine without comparing.
Code Sample - LINQ Concat Product Comparer
Code Sample - LINQ Concat With Product Comparer
Summary
In this article we learn't how to combine data between collections using Concat. This can be used to combine items between collection and return a unified collection with duplicates. All these can be used with any IEnumerable or IQueryable types.