Convert string array to custom object list using linq
16,297
I would use Linq's Select extension and ToList to convert the IEnumerable (from Select extension) to a list
An Example:
string[] randomArray = new string[3] {"1", "2", "3"};
List<CustomEntity> listOfEntities = randomArray.Select(item => new CustomEntity() { FileName = item } ).ToList();
Related videos on Youtube
Author by
Neeraj Kumar Gupta
Updated on June 19, 2022Comments
-
Neeraj Kumar Gupta almost 2 years
I have a class with one public property.
public class CustomEntity { string _FileName; public string FileName { get { return _FileName; } set { _FileName = value; } } }
I have array of string which I want to convert in List of "CustomEntity" using linq. please suggest me how I can do that.
-
Jon Skeet about 11 yearsWell, how much research have you done? What's the purpose of your Instance property (which confusingly will return a new instance on each call)? Note that not having an accessible constructor will mean you can't use object initializers.
-
-
WinW over 5 yearswouldn't it be sufficient enough to use just: List<CustomEntity> = randomArray.ToList()