Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Combining Collections | Sets and Collection Utilities
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Lists & Collections

bookCombining Collections

Combining collections is a common task in C#. Whether you are merging lists of items, uniting sets of unique values, or joining dictionaries based on shared keys, the .NET framework provides several methods to help. Three primary methods you will use are Concat, Union, and Join.

  • Concat: combines two sequences by appending one after the other, keeping all elements, including duplicates;
  • Union: combines two sequences and removes any duplicate elements, resulting in only unique items;
  • Join: connects elements from two collections based on a matching key, useful for combining dictionaries or lists of objects with shared properties.

Understanding these methods helps you choose the right approach for your data and ensures you do not lose or duplicate information unintentionally.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> listA = new List<string> { "apple", "banana", "cherry" }; List<string> listB = new List<string> { "banana", "date", "fig" }; // Merge both lists List<string> merged = new List<string>(); merged.AddRange(listA); merged.AddRange(listB); // Remove duplicates using a HashSet HashSet<string> uniqueSet = new HashSet<string>(merged); Console.WriteLine("Merged list with duplicates:"); foreach (var item in merged) { Console.WriteLine(item); } Console.WriteLine("\nUnique items after removing duplicates:"); foreach (var item in uniqueSet) { Console.WriteLine(item); } } } }

When combining collections, you should think about your data and the outcome you want. If you simply append one list to another, you may end up with duplicate entries. Using a set, like HashSet<T>, ensures all values are unique after merging. This is especially useful for things like user IDs, product codes, or any case where duplicates would cause problems.

On the other hand, when working with dictionaries, joining them by key allows you to merge related data without losing information. However, you must be careful—if both dictionaries have the same key, you need to decide which value to keep or how to combine them. Sometimes, using Union with sets or careful joins with dictionaries is necessary to avoid accidental data loss or overwriting important information.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main(string[] args) { var dict1 = new Dictionary<int, string> { { 1, "Alice" }, { 2, "Bob" }, { 3, "Charlie" } }; var dict2 = new Dictionary<int, string> { { 2, "Bobby" }, { 3, "Charlotte" }, { 4, "Diana" } }; // Join dictionaries by key var joined = dict1.Join( dict2, d1 => d1.Key, d2 => d2.Key, (d1, d2) => new { Key = d1.Key, Value1 = d1.Value, Value2 = d2.Value } ); Console.WriteLine("Joined dictionary entries by key:"); foreach (var item in joined) { Console.WriteLine($"Key: {item.Key}, Value1: {item.Value1}, Value2: {item.Value2}"); } } } }

1. Which method combines two lists into one?

2. How can you remove duplicates when merging collections?

3. Fill in the blanks to join two dictionaries by key.

question mark

Which method combines two lists into one?

Select the correct answer

question mark

How can you remove duplicates when merging collections?

Select the correct answer

question-icon

Fill in the blanks to join two dictionaries by key.

var result = dictA.( dictB, , , );

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show me code examples for merging lists, sets, and dictionaries in C#?

What are some common mistakes to avoid when combining collections?

How do I decide whether to use Concat, Union, or Join for my data?

bookCombining Collections

Svep för att visa menyn

Combining collections is a common task in C#. Whether you are merging lists of items, uniting sets of unique values, or joining dictionaries based on shared keys, the .NET framework provides several methods to help. Three primary methods you will use are Concat, Union, and Join.

  • Concat: combines two sequences by appending one after the other, keeping all elements, including duplicates;
  • Union: combines two sequences and removes any duplicate elements, resulting in only unique items;
  • Join: connects elements from two collections based on a matching key, useful for combining dictionaries or lists of objects with shared properties.

Understanding these methods helps you choose the right approach for your data and ensures you do not lose or duplicate information unintentionally.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> listA = new List<string> { "apple", "banana", "cherry" }; List<string> listB = new List<string> { "banana", "date", "fig" }; // Merge both lists List<string> merged = new List<string>(); merged.AddRange(listA); merged.AddRange(listB); // Remove duplicates using a HashSet HashSet<string> uniqueSet = new HashSet<string>(merged); Console.WriteLine("Merged list with duplicates:"); foreach (var item in merged) { Console.WriteLine(item); } Console.WriteLine("\nUnique items after removing duplicates:"); foreach (var item in uniqueSet) { Console.WriteLine(item); } } } }

When combining collections, you should think about your data and the outcome you want. If you simply append one list to another, you may end up with duplicate entries. Using a set, like HashSet<T>, ensures all values are unique after merging. This is especially useful for things like user IDs, product codes, or any case where duplicates would cause problems.

On the other hand, when working with dictionaries, joining them by key allows you to merge related data without losing information. However, you must be careful—if both dictionaries have the same key, you need to decide which value to keep or how to combine them. Sometimes, using Union with sets or careful joins with dictionaries is necessary to avoid accidental data loss or overwriting important information.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main(string[] args) { var dict1 = new Dictionary<int, string> { { 1, "Alice" }, { 2, "Bob" }, { 3, "Charlie" } }; var dict2 = new Dictionary<int, string> { { 2, "Bobby" }, { 3, "Charlotte" }, { 4, "Diana" } }; // Join dictionaries by key var joined = dict1.Join( dict2, d1 => d1.Key, d2 => d2.Key, (d1, d2) => new { Key = d1.Key, Value1 = d1.Value, Value2 = d2.Value } ); Console.WriteLine("Joined dictionary entries by key:"); foreach (var item in joined) { Console.WriteLine($"Key: {item.Key}, Value1: {item.Value1}, Value2: {item.Value2}"); } } } }

1. Which method combines two lists into one?

2. How can you remove duplicates when merging collections?

3. Fill in the blanks to join two dictionaries by key.

question mark

Which method combines two lists into one?

Select the correct answer

question mark

How can you remove duplicates when merging collections?

Select the correct answer

question-icon

Fill in the blanks to join two dictionaries by key.

var result = dictA.( dictB, , , );

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 6
some-alt