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

后端 未结 5 1189
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:25

    IEnumerable e = (from char c in source
                            select new { Data = c.ToString() }).Select(t = > t.Data);
    // or
    IEnumerable e = from char c in source
                            select c.ToString();
    // or
    IEnumerable e = source.Select(c = > c.ToString());
    

    Then you can call ToList():

    List l = (from char c in source
                      select new { Data = c.ToString() }).Select(t = > t.Data).ToList();
    // or
    List l = (from char c in source
                      select c.ToString()).ToList();
    // or
    List l = source.Select(c = > c.ToString()).ToList();
    

提交回复
热议问题