c# - Update all objects in a list from objects in another list -


i have following 2 objects:

public class blogpost     {         public int blogpostid { get; set; }         public author author { get; set; }     }  public class author     {         public int authorid { get; set; }         public string firstname { get; set; }         public string lastname { get; set; }     } 

i have 2 separate lists of 50 blog post objects , 50 author objects. how combine 2 such author object assigned blog post's author property?

i've tried using zip method, don't think that's quite want. question similar, want value object list.

context: using seed method in entityframework populate database.

edit: should have mentioned that, because seed data, don't care author gets matched blogpost. hence why trying use zip method.

if blogpost , author objects linked via index can use for-loop:

for(int = 0; < blogposts.count; i++)     blogposts[i].author = authors[i]; 

you use linq approaches zip have create new objects old , fill new list. for-loop more appropriate (and efficient) if want modify original collection. can modify single property without needing copy all.

so works not best approach (consider blogpost had 100 properties):

blogposts = blogposts.zip(authors, (bp, a) => new blogpost {     blogpostid = bp.blogpostid,     author = }).tolist(); 

but requirement brings question why didn't initialized blogpost-list correctly in first place. why need 2 separate collections @ all?


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -