Home > database >  Return IEnumerable<'a>from function
Return IEnumerable<'a>from function

Time:11-26

Tell me, please, how can I return an object with the type IEnumerable<'a>? My code should return a list of objects with nested objects, for example:

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

Now I get a compilation error, and I can't find the type of method for it:

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?)

My Code:

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

CodePudding user response:

You can return named tuple, say, (string company, Phone[] phones) instead of anonymous class:

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

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

CodePudding user response:

The IEnumerable you return isn't of the type of phone its more a Dictionary<string,IEnumerable<Phone>>

or use anouther class like 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()
    }
}
  • Related