How to join two lists together in C#, Convert List<string> to a comma separated string in C#, Concat a specific string property from a list of objects in C#, Join List<List<String>> into a single string with delimiter in C#

Concat string from List in C#

Here are some examples of working with lists in C#:

Example 1: Joining Two Lists Together

List<int> list1 = new List<int> { 1, 2, 3 }; List<int> list2 = new List<int> { 4, 5, 6 }; List<int> combinedList = list1.Concat(list2).ToList(); 

In this example, two List<int> objects are created, and then they are concatenated using the Concat method. The resulting combined list is then converted to a new List<int> using the ToList method.

Example 2: Converting List<string> to Comma Separated String

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Dave" }; string commaSeparatedNames = string.Join(",", names); 

In this example, a List<string> is initialized with several string values, and then the list is converted to a comma-separated string using the string.Join method.

Example 3: Concatenating a Specific String Property from a List of Objects

class Person { public string Name { get; set; } public int Age { get; set; } } List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 20 }, new Person { Name = "Charlie", Age = 25 } }; string commaSeparatedNames = string.Join(",", people.Select(p => p.Name)); 

In this example, a list of Person objects is created, and then the Name property of each object is concatenated into a comma-separated string using the Select method and the string.Join method.

Example 4: Joining List<List<String>> into a Single String with Delimiter

List<List<string>> listOfLists = new List<List<string>> { new List<string> { "Alice", "Bob", "Charlie" }, new List<string> { "Dave", "Eve" }, new List<string> { "Frank" } }; string delimiter = "|"; string joinedString = string.Join(delimiter, listOfLists.Select(l => string.Join(",", l))); 

In this example, a List<List<string>> is initialized with several lists of string values. The Select method is used to project each inner list to a comma-separated string, and then the outer list is joined into a single string using the string.Join method with a specified delimiter.

Examples

  1. C# concatenate strings from List:

    List<string> stringList = new List<string> { "Hello", " ", "World" }; string concatenatedString = string.Concat(stringList); 
  2. Joining strings in a List in C#:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string sentence = string.Join(" ", words); 
  3. Concatenation of elements in List<string> C#:

    List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = items.Aggregate((current, next) => current + next); 
  4. C# LINQ concatenate strings in List:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Concat(words.Select(word => word + " ")); 
  5. Concatenating List of objects to string in C#:

    List<Person> persons = new List<Person> { /* List of Person objects */ }; string concatenatedNames = string.Join(", ", persons.Select(person => person.Name)); 
  6. C# StringBuilder concatenate strings from List:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; StringBuilder sb = new StringBuilder(); words.ForEach(word => sb.Append(word)); string result = sb.ToString(); 
  7. Joining elements in List<string> with delimiter in C#:

    List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = string.Join(", ", items); 
  8. C# concatenate strings with newline from List:

    List<string> lines = new List<string> { "Line 1", "Line 2", "Line 3" }; string concatenatedLines = string.Join(Environment.NewLine, lines); 
  9. C# concatenate strings in List with separator:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Join(" - ", words); 
  10. Concatenating strings from List with foreach loop in C#:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string result = ""; foreach (var word in words) { result += word; } 
  11. C# concatenate strings in List using + operator:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Empty; foreach (var word in words) { concatenatedString += word; } 
  12. Join List<string> to comma-separated string in C#:

    List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string result = string.Join(",", items); 
  13. C# concatenate strings from List with specific condition:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = string.Concat(words.Where(word => word.Length > 1)); 
  14. Concatenating List<string> using String.Concat in C#:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string concatenatedString = String.Concat(words); 
  15. C# LINQ join List<string> to a single string:

    List<string> words = new List<string> { "This", "is", "a", "sentence" }; string result = string.Join(" ", words); 
  16. Concatenate unique strings from List<string> in C#:

    List<string> items = new List<string> { "Item1", "Item2", "Item1", "Item3" }; string result = string.Join(", ", items.Distinct()); 

More Tags

bigdecimal periodicity levenshtein-distance kendo-tabstrip printf struct native-base iso8601 mkannotation preflight

More Programming Guides

Other Guides

More Programming Examples