Reputation: 133
I have 2 Classes lets say ParentClass and ChildClass
public class ParentClass
{
public int Property1 {get; set;}
public List<Child> {get; set;}
}
public class ChildClass
{
public int Property1 {get; set;}
public string Property2 {get; set;}
}
Now i have an object of parent class
// in my case all data has been assign to parent object properties except List<ChildClass>
List<ParentClass > parent = data
How can i assign data to List property of parent list without using Loop.
Thanks
Upvotes: 0
Views: 2106
Reputation: 236248
LINQ (Language-Integrated Query) is for querying. And querying is a thing which does not supposed to cause side-effects. Modifying objects is a pure side-effect. So, Linq is just inappropriate tool here. Even more - internally LINQ uses loops, so there will not be any performance benefit also.
Suggestion - use simple loop to update your objects - its very simple construct which is easy to understand:
foreach(var parent in parents)
parent.Children = GetChildren(parent.Id);
Without explicit loop you can use List.ForEach
:
parents.ForEach(p => p.Children = GetChildren(parent.Id));
But I don't like this approach - why write something that looks like foreach, if you can use foreach? Also introducing lambda adds complexity to your system.
but as CreateMap method of Automapper is not threadsafe
With Automapper you usually create maps on application startup (Main
method on Winforms, or Global.asax
on web - both are executed on single thread). Also you always can wrap maps creation with lock
statement, as with any other code which needs thread synchronization.
Upvotes: 0
Reputation: 1593
Cannot really understand what you want to do.
Maybe,
var parent = data.Select(p => new ParentClass{Property1 = p.Property1}).ToList();
You'll get copy of data, but List<Child> property will not be copied.
Upvotes: 1