1

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();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MIKE
  • 1,039
  • 7
  • 24
  • That is not the same question or answer. I do not agree with the duplicate. Maybe you made a mistake? – MIKE Nov 29 '15 at 18:21

1 Answers1

2

As clear in the error message, Entity Framework does not like the fact that you are using the same class (i.e., DropDownSelect) in two places inside the query and each time you are setting different properties. That is, in the first time, you are setting Id, Name, and Next, but the second time your are setting Id and Name only.

To fix this, use an anonymous type in the query, and later convert it to DropDownSelect like this:

public static async Task<List<DropDownSelect>> SelectReportDropDown(bool isPpm, int companyId = 0)
{
    using (var context = ContextFactory.getLiveConnection())
    {
        var result = await context.Companies.Select(c => new // this is an anonymous type
        {
            Id = c.ID,
            Name = c.Name,
            Next = c.Reports.Select(p => new DropDownSelect //DropDownSelect is now used in only one place in the query
            {
                Id = p.ProjectId,
                Name = p.Name,
            }).ToList()
        }).ToListAsync();

        return result.Select(x => new DropDownSelect
        {
            Id = x.Id,
            Name = x.Name,
            Next = x.Next
        }).ToList();


    }
}
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62