Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

c# - Error System.Collections.Generic.List`1[System.String] when itry String.Join on a list of result

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:

"
                + String.Join(",", $"

{resultList1}

");
question from:https://stackoverflow.com/questions/65853173/error-system-collections-generic-list1system-string-when-itry-string-join-on

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the string join like ths,

var message = "List strings not found:

" + String.Join(",", resultList1);

You are using the system join on an enumerable array but when you use $" {result1} " 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...