1

I'm trying to fill a list within a list using LINQ to query my database.

The issue I'm facing is that I'm unsure how to select the data into the child list.

When trying to execute the code below, I receive the error

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

The model classes are like so:

public class _LayoutViewModel
{
    public List<CallGuideMenuL1> CGL1 { get; set; }
}

public class CallGuideMenuL1
{
    public string Area { get; set; }
    public List<CallGuideMenuL2> Products { get; set; }
}

public class CallGuideMenuL2
{
    public int CallGuideProductId { get; set; }
    public string Product { get; set; }
}

And the DB context:

public class CallGuideArea
{
    public int CallGuideAreaId { get; set; }
    public string Area { get; set; }
    public List<CallGuideProduct> CallGuideProducts { get; set; }
}

public class CallGuideProduct
{
    public int CallGuideProductId { get; set; }
    public string Product { get; set; }
    public int CallGuideAreaId { get; set; }
    public DateTime Added { get; set; }
    public DateTime? Deleted { get; set; }
}

In my controller I'm trying to select the data like so:

_LayoutViewModel vm = new _LayoutViewModel();

vm.CGL1 = from a in db.CallGuideArea
            .SelectMany(p => p.CallGuideProducts)
            select a;

I'm pretty sure it's the select a; line that's the issue as I need to assign the data back to the properties of both CallGuideMenuL1 and CallGuideMenuL2.

Could anyone point me in the right direction around the right LINQ expression?

4
  • if vm.CGL1 is IList<CallGuideProducts> you can try with vm.CGL1 = db.CallGuideArea.SelectMany(p=>p.CallGuideProducts).ToList(); Commented Apr 18, 2015 at 15:00
  • @GiorgiNakeuri vm.CGL1 is the view model class, I've updated the question to include it sorry. Commented Apr 18, 2015 at 15:04
  • Are you trying to map CallGuideArea objects to CallGuideMenuL1? Commented Apr 18, 2015 at 15:07
  • @RodrigoJuarez yes that's exactly it thanks. Commented Apr 18, 2015 at 15:07

2 Answers 2

3
vm.CGL1 = db.CallGuideArea.Select(a => new CallGuideMenuL1()
{
    Area = a.Area,
    Products = a.CallGuideProducts.Select(p => new CallGuideMenuL2()
    {
        CallGuideProductId = p.CallGuideProductId,
        Product = p.Product
    }).ToList()
}).ToList();
Sign up to request clarification or add additional context in comments.

Comments

0

Probably vm.CGL1 is declared as List so you need to select into List:

vm.CGL1 = (from a in db.CallGuideArea
            .SelectMany(p => p.CallGuideProducts)
            select a).ToList();

or you will need to project:

vm.CGL1 = (from a in db.CallGuideArea
            .SelectMany(p => p.CallGuideProducts)
            select new CallGuideMenuL1()
            {
               Area = a.--some property
               ...
            }).ToList();

1 Comment

Thanks for your answer. The only issue with the projection is that a doesn't include the data from CallGuideArea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.