无法隐式转换类型“System.Collections.Generic.IEnumerable<AnonymousType#1>"到“System.Collections.Generic.List<string>" [英] Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<string>
本文介绍了无法隐式转换类型“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>();
编译时报错
无法将类型 'System.Collections.Generic.List
隐式转换为 'System.Collections.Generic.List
>
Cannot implicitly convert type
'System.Collections.Generic.List<AnonymousType#1>'
to'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();
这篇关于无法隐式转换类型“System.Collections.Generic.IEnumerable<AnonymousType#1>"到“System.Collections.Generic.List<string>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文