10

I am attempting to build up a hierarchical collection using Entity Framework - see the below query - every member in a given company has a parent member - but when trying to execute this I get the following exception:

System.NotSupportedException: The type 'Member' 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.

If I remove the ParentMember assign it works - any ideas on what is going on?

        return from c in _Entities.Company
               where c.Deleted == false
                select new Member()
                {
                    Name = c.Name,
                    ParentMember = new Member() 
                    {
                        Name = c.ParentMember.Name
                    }
                }; 

3 Answers 3

12

I haven't tried this, but the error message gives you a clue: you're not setting the same properties in the same order in both places.

What happens if you try setting an ID property on the outer Member()?

Sign up to request clarification or add additional context in comments.

2 Comments

I've just updated the code in the question to reflect the actual code - I have exactly the same properties being set in the same order with the exception of setting the ParentMember property of the ParentMember, if that makes sense.
Actually, you're still not setting all the same params: the outer has Name and ParentMember, while you're only setting the Name for the inner. Try assigning the property a null.
2

Try

return (from c in _Entities.Company
               where c.Deleted == false
                select new
                {
                    c.Name,
                    ParentMember = new
                    {
                        c.ParentMember.Name
                    }
                })
.AsEnumerable()
.Select(c=> new Member
                {
                    Name = c.Name,
                    ParentMember = new Member
                    {
                        Name = c.ParentMember.Name
                    }
                }); 

1 Comment

+1 This catch-22 situation just bit me, and this was the solution I came up with independently. Less elegant than I was hoping, and a little wasteful, since we have to do double the object instantiation, but nonetheless effective.
1

You will end up with a recursion of Member records when you try to retrieve the same fields in each one. You can't just make the last parent record equal to null.

I would retrieve what I could and then build up the record with further queries. Note that your Company entity will require a ParentId field or similar.

var members = 
  return from c in _Entities.Company
  select new Member()
  {
    Name = c.Name,
    ParentId = c.ParentId
  }; 

Now iterate and add in the parent records.

foreach (var member in members)
{
  member.ParentMember = new Member 
    {
      Name = _Entities.Company.First(c => c.Id == member.ParentId).Name
    };
}

Comments

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.