1

I am getting the error System.Collections.Generic.List`1[System.String] when i try to use String.Join in a list of result.

For instance i have two list like this:

var list1= new List<string>{"string1","string2"};
var list2= new List<string>{"string1"};

Then i want to get a message with the string that doesn't appear on list2

var resultList1 = list1.Except(list2).ToList(); // this line get a list with "string2"

when i use String.Join i get the error System.Collections.Generic.List`1[System.String]. Also i tried resultList1.Cast<List>() instead resultList1 with same outcome.

var message = "List strings not found:\n\n"
                + String.Join(",", $"\n\n{resultList1}\n\n");
2
  • I'm not sure what exactly the error message is, but my best guess is that the second parameter of string.Join() needs to be a collection. Commented Jan 22, 2021 at 21:47
  • Please take the tour, read How to Ask and learn to use formatting. Commented Jan 22, 2021 at 21:51

1 Answer 1

1

You need to use the string join like ths,

var message = "List strings not found:\n\n" + String.Join(",", resultList1);

You are using the system join on an enumerable array but when you use $"\n\n{result1}\n\n" string interpolation, you are basically joining a single string which doesn't work.

String.Join takes in a string (comma, newline or any string) that combines all elements from an array but when you give string instead of array or collection, it will give the error.

Lastly, Documentation should help explain more about the usage in depth.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you asnwer. It was really helpful to me
@Andres if the post answered your question, please be sure to mark it as answered by clicking the ✔ under the vote count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.