Return IEnumerable<'a>来自函数

Return IEnumerable<'a>from function

请告诉我,我怎样才能 return 一个类型为 IEnumerable<'a> 的对象? 我的代码应该 return 具有嵌套对象的对象列表,例如:

[0] LG
{
  [0].LG G3
  [1].LG G4
}
[1] Xiaomi
{
...
}

现在我遇到编译错误,找不到它的方法类型:

Error   CS0266  Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<<anonymous type: string Name, System.Collections.Generic.IEnumerable<ConsoleApp1.Phone> Phones>>' to 'System.Collections.Generic.IEnumerable<ConsoleApp1.Phone>'. An explicit conversion exists (are you missing a cast?)

我的代码:

class Phone
{
    public string Name { get; set; }
    public string Company { get; set; }
}

class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<Phone> GetInfo()
            {
                List<Phone> phones = new List<Phone>()
            {
                    new Phone {Name="Lumia 430", Company="Microsoft" },
                    new Phone {Name="Mi 5", Company="Xiaomi" },
                    new Phone {Name="LG G 3", Company="LG" },
                    new Phone {Name="iPhone 5", Company="Apple" },
                    new Phone {Name="Lumia 930", Company="Microsoft" },
                    new Phone {Name="iPhone 6", Company="Apple" },
                    new Phone {Name="Lumia 630", Company="Microsoft" },
                    new Phone {Name="LG G 4", Company="LG" }
            };

                var result = (from phone in phones
                        group phone by phone.Company into g
                        select new
                        {
                            Name = g.Key,
                            Phones = from p in g select p
                        });

                return result;
            }

            //my code with GetInfo()
        }
    }

你可以return 命名元组,比如说,(string company, Phone[] phones)而不是匿名class :

IEnumerable<(string company, Phone[] phones)> GetInfo() {
  ...

  return phones
    .GroupBy(phone => phone.Company)
    .Select(group => (group.Key, group.ToArray())); 
}  

您 return 的 IEnumerable 不是 phone 的类型,它更像是 Dictionary<string,IEnumerable<Phone>>

或使用另一个 class,例如 PhoneInfo

public class Phone
{
    public string Name { get; set; }
    public string Company { get; set; }
}
public class PhoneInfo
{
    public string Name { get; set; }
    public List<Phone> Phones { get; set; } 
}

class Program
{
    static void Main(string[] args)
    {
        
        IEnumerable<PhoneInfo> GetInfo()
        {
            List<Phone> phones = new List<Phone>()
            {
                    new Phone {Name="Lumia 430", Company="Microsoft" },
                    new Phone {Name="Mi 5", Company="Xiaomi" },
                    new Phone {Name="LG G 3", Company="LG" },
                    new Phone {Name="iPhone 5", Company="Apple" },
                    new Phone {Name="Lumia 930", Company="Microsoft" },
                    new Phone {Name="iPhone 6", Company="Apple" },
                    new Phone {Name="Lumia 630", Company="Microsoft" },
                    new Phone {Name="LG G 4", Company="LG" }
            };

            var result = (from phone in phones
                          group phone by phone.Company into g
                          select new PhoneInfo
                          {
                              Name = g.Key,
                              Phones = (from p in g select p).ToList()
                          });

            return result;
        }

        //my code with GetInfo()
    }
}