无法隐式转换类型“System.Collections.Generic.IEnumerable”到“System.Collections.Generic.List<字符串>”

发布于 2024-10-04 07:59:56 字数 608 浏览 0 评论 0原文

我有下面的代码:

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'

需要帮助。

I have the code below:

List<string> aa = (from char c in source
                   select new { Data = c.ToString() }).ToList();

But what about

List<string> aa = (from char c1 in source
                   from char c2 in source
                   select new { Data = string.Concat(c1, ".", c2)).ToList<string>();

While compile getting error

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

Need help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杯别 2024-10-11 07:59:56
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();
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());

Then you can call 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();
杀手六號 2024-10-11 07:59:56

如果您希望它是 List,请删除匿名类型并添加 .ToList() 调用:

List<string> list = (from char c in source
                     select c.ToString()).ToList();

If you want it to be List<string>, get rid of the anonymous type and add a .ToList() call:

List<string> list = (from char c in source
                     select c.ToString()).ToList();
赢得她心 2024-10-11 07:59:56

尝试

var lst= (from char c in source select c.ToString()).ToList();

try

var lst= (from char c in source select c.ToString()).ToList();
方圜几里 2024-10-11 07:59:56

如果您有像 "abcd" 这样的字符串源并且想要生成如下列表:

{ "a.a" },
{ "b.b" },
{ "c.c" },
{ "d.d" }

然后调用:

List<string> list = source.Select(c => String.Concat(c, ".", c)).ToList();

If you have source as a string like "abcd" and want to produce a list like this:

{ "a.a" },
{ "b.b" },
{ "c.c" },
{ "d.d" }

then call:

List<string> list = source.Select(c => String.Concat(c, ".", c)).ToList();
似梦非梦 2024-10-11 07:59:56

我认为答案如下

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();

I think the answers are below

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文