I am trying to build a generic dropdownlist view model that can have many drop down lists inside it.
When I use Entity Framework to select the dropdownlist vm and inside it select another list of dropdownlist vm I get an error. However if I change my code to have 2 vms instead of using 1 generic 1 everything works fine. I am trying to avoid extra classes and keep the same viewmodels consistent across all my projects.
Error:
DropDownSelect' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
ViewModel:
public class DropDownSelect
{
public int Id { get; set; }
public string Name { get; set; }
public List<DropDownSelect> Next { get; set; }
public DropDownSelect()
{
Next = new List<DropDownSelect>();
}
}
Linq:
public static async Task<List<DropDownSelect>> SelectReportDropDown(bool isPpm, int companyId = 0)
{
using (var context = ContextFactory.getLiveConnection())
{
return await context.Companies.Select(c => new DropDownSelect()
{
Id = c.ID,
Name = c.Name,
Next = c.Reports.Select(p => new DropDownSelect()
{
Id = p.ProjectId,
Name = p.Name,
}).ToList()
}).ToListAsync();
}
}