Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List

后端 未结 5 1188
Happy的楠姐
Happy的楠姐 2020-11-29 09:21

I have the code below:

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

But

相关标签:
5条回答
  • 2020-11-29 09:24

    try

    var lst= (from char c in source select c.ToString()).ToList();
    
    0 讨论(0)
  • 2020-11-29 09:25
    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();
    
    0 讨论(0)
  • 2020-11-29 09:28

    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();
    
    0 讨论(0)
  • 2020-11-29 09:41

    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();
    
    0 讨论(0)
  • 2020-11-29 09:42

    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();
    
    0 讨论(0)
提交回复
热议问题