Monday, November 5, 2018

Explain it to me like I'm 5 please. Children items null after updating the Id of the parent.

Hey guys, I'm feeling pretty stupid today and not understanding why this is happening. I just need someone to explain it to me like I'm 5. Here is the problem:

I have two classes that looks like this:

public class ParentItem { public string Id {get;set;} public string Name {get;set;} public IEnumerable<ChildItem> Children {get;set;} } public class ChildItem { public string Id {get;set;} public string ParentItemId {get;set;} public string Name {get;set;} } 

So I have a collection of ParentItems and then I iterate through them updating their Id's to a new id. In doing so the collection of Children becomes null.

foreach(var p in ParentItems) { p.Id = Guid.NewGuid().ToString() } 

It is here that the p.Children becomes null. If I just update the p.Name the collection remains populated. This collection comes from a repository call that uses Dapper.

I feel like I need some basic school on collections in C#. Quite embarrassed by it in fact.

Explain it to me like I'm 5 please. Children items null after updating the Id of the parent. Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team