0

I have a special problem...

Szenario: I have a Contentype, which has two Entity-Fields. One of the Entityfields ist filled, one is empty.I got the data (As DynamicEntity named result1). For the empty entityfield, i got a default value in a Method (as DynamicEntity named result2). Now i want to add result2 in result1 for the empty property. The add-method throw no error but the property remains null...

Example: The contenttype is named Header.

Fields: Titel (string), Font (entityfield), Color (entityfield)

Data: "Title", Selected one item from Contenttype Font, nothing selected.

I fetch the data into result1. result1 is from type ToSic.SexyContent.DynamicEntity

result1.Font is type System.Collections.GenericList`1[ToSic.SexyContent.DynamicEntity] and i can access result1.Font[0].Name and got the value

result1.Color is type System.Collections.GenericList`1[ToSic.SexyContent.DynamicEntity] and value null (result1.Color.Count give me 0)

result2 is from type ToSic.SexyContent.DynamicEntity

Now i want to add result2 with result1.Color.Add(result2); to result1. No error but nothing happens. When i try to access result1.Color[0].name i got the error System.ArgumentOutOfRangeException...

Any Hints?

  • This feels confusing. Please provide more details where you're trying to do this. Is it in a razor-template, in a web-api? Are you trying to merge lists for output, or are you trying to modify data in the db? – iJungleBoy Dec 12 '16 at 13:40
  • It's in a razor template and i am trying to merge lists for output. Only normal stuff... ;-) – Andreas Flohr Dec 12 '16 at 18:44

1 Answers1

0

From what I understand you have two lists of entities, and want to merge them to then output them later on.

If I also understand you correctly, one of the lists is a property on an existing entity - something like Team.Members - and you tried to add the other entities - let's say Team.Leaders - to the Team.Members collection, correct?

In this case your mistake is to try to modify an entity-property and enhancing it, because entities and their data are read-only.

All you have to do is create a new list/collection of these entities, and loop through that. You can do this manually like List<DynamicEntity> xyz = new List<DynamicEntity>(); and adding things, or you could play around with LINQ to do the same.

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
  • oh, i did not see the readonly ... i have solved my problem with an ExpandoObject, with gets the values from both DynamicEntitys. Thank you! – Andreas Flohr Dec 15 '16 at 14:37