B.Balamanigandan
B.Balamanigandan

Reputation: 4875

How to Cast Linq Select Enumerable to preferred type in C#?

I'm having an ObservableCollection namely MobileList of type MobileModel. The MobileModel has a List of MobileModelInfo.

I need only MobileModelInfo from that MobileList. So I used Linq Select Statement in the method ExtractFirstMobileList(). But I can't able to assign the List, it shows an error.

My C# Source Code:

public class Mobile
{
    private ObservableCollection<MobileModel> _mobileList;
    public ObservableCollection<MobileModel> MobileList
    {
        get { return _mobileList; }
        set { _mobileList = value; OnPropertyChanged(); }
    }

    public void GetMobile()
    {
        List<MobileModel> mList = new List<MobileModel>();
        List<MobileModelInfo> modList = new List<MobileModelInfo>();
        MobileModel mob = new MobileModel();

        modList.Clear();
        mob.Brand = "Apple";
        modList.Add(new MobileModelInfo { Name = "iPhone 4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "iPhone 5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "iPhone 6", Catagory = "Premium Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "IOS";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Samsung";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "Ultra Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "MicroSoft";
        modList.Add(new MobileModelInfo { Name = "Lumina 9900", Catagory = "Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "Opera X220", Catagory = "Smart Phone", Year = "2013" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Windows";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Sony Ericssion";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "Ultra Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);

        MobileList = new ObservableCollection<MobileModel>(mList);

        ExtractFirstMobileList(MobileList);

    }


    public void ExtractFirstMobileList(ObservableCollection<MobileModel> Source)
    {
        List<MobileModelInfo> mInfo = Source.Select(t=> t.Model);
    }

}

public class MobileModel : Notify
{
    private string _brand = string.Empty;
    private List<MobileModelInfo> _model = new List<MobileModelInfo>();
    private string _os = string.Empty;

    public string Brand
    {
        get { return _brand; }
        set { _brand = value; OnPropertyChanged(); }
    }
    public List<MobileModelInfo> Model
    {
        get { return _model; }
        set { _model = value; OnPropertyChanged(); }
    }

    public string OS
    {
        get { return _os; }
        set { _os = value; OnPropertyChanged(); }
    }
}

public class MobileModelInfo
{
    public string Name { get; set; }
    public string Catagory { get; set; }
    public string Year { get; set; }
}

Kindly assist me how to cast the Enumerable to preferred type.

Upvotes: 1

Views: 211

Answers (3)

Zrethreal
Zrethreal

Reputation: 338

If you want to create a flattened list with all the different MobileModelInfo you can use SelectMany:

List<MobileModelInfo> mInfo = Source.SelectMany(t => t.Model).ToList();

But your method is called ExtractFirstMobileList, which suggests that you want the first item in each sublist - in that case you can use:

List<MobileModelInfo> mInfo = Source.Select(t => t.Model.First()).ToList();

Hope this helps

Upvotes: 5

Andrew
Andrew

Reputation: 1534

If you need to extreact a list with LINQ use ToList() method after Select() method. Also be sure that your list is not empty, it exists and contains more than 0 elements

Upvotes: 1

chijos
chijos

Reputation: 111

The Model property on the MobileModel class is a list itself. So the select query you've used will return a collection of collections of MobileModelInfo (return type IEnumerable<List<MobileModelInfo>>)

To convert this to a List<List<MobileModelInfo>> add a .ToList() at the end.

List<List<MobileModelInfo>> mInfo = Source.Select(t => t.Model).ToList();

Upvotes: 3

Related Questions