Suppose we have a user class
public class User { public string Name { get; set; } }and we have the usernames in a string array as shown below
string[] names = new string[5] {"John", "David", "Robert", "Paul", "Mary" };if we want to convert it as list of User class objects, the general method for this is
ListUsing LINQ, we can achive the same with a single lineusersList =new List (); foreach(var name in names) { usersList.Add(new User(){ Name=name; }); }
ListHappy Coding 😊 !!usersList = names.Select(x => new User() { Name = x } ).ToList();
0 comments:
Post a Comment