不能将types'System.Collections.Generic.IEnumerable <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>
我有下面的代码:
List<string> aa = (from char c in source select new { Data = c.ToString() }).ToList();
但是关于
List<string> aa = (from char c1 in source from char c2 in source select new { Data = string.Concat(c1, ".", c2)).ToList<string>();
编译得到错误
不能将types
'System.Collections.Generic.List<AnonymousType#1>'
隐式转换为'System.Collections.Generic.List<string>'
需要帮忙。
IEnumerable<string> e = (from char c in source select new { Data = c.ToString() }).Select(t = > t.Data); // or IEnumerable<string> e = from char c in source select c.ToString(); // or IEnumerable<string> e = source.Select(c = > c.ToString());
然后你可以调用ToList()
:
List<string> l = (from char c in source select new { Data = c.ToString() }).Select(t = > t.Data).ToList(); // or List<string> l = (from char c in source select c.ToString()).ToList(); // or List<string> l = source.Select(c = > c.ToString()).ToList();
如果你希望它是List<string>
,那么去掉匿名types并添加一个.ToList()
调用:
List<string> list = (from char c in source select c.ToString()).ToList();
尝试
var lst= (from char c in source select c.ToString()).ToList();
如果你的源代码是像"abcd"
这样的string,并且想产生一个像这样的列表:
{ "aa" }, { "bb" }, { "cc" }, { "dd" }
然后打电话:
List<string> list = source.Select(c => String.Concat(c, ".", c)).ToList();
我认为答案如下
List<string> aa = (from char c in source select c.ToString() ).ToList(); List<string> aa2 = (from char c1 in source from char c2 in source select string.Concat(c1, ".", c2)).ToList();