【发布时间】:2021-01-22 21:45:39
【问题描述】:
当我尝试在结果列表中使用 String.Join 时,我收到错误 System.Collections.Generic.List`1[System.String]。
例如,我有两个这样的列表:
var list1= new List<string>{"string1","string2"};
var list2= new List<string>{"string1"};
然后我想收到一条消息,其中包含未出现在 list2 上的字符串
var resultList1 = list1.Except(list2).ToList(); // this line get a list with "string2"
当我使用 String.Join 时,我收到错误 System.Collections.Generic.List`1[System.String]。我也尝试了 resultList1.Cast() 而不是 resultList1 具有相同的结果。
var message = "List strings not found:\n\n"
+ String.Join(",", $"\n\n{resultList1}\n\n");
【问题讨论】:
-
我不确定错误 message 到底是什么,但我最好的猜测是
string.Join()的第二个参数需要是一个集合。 -
请收下tour,阅读How to Ask,学习使用formatting。