Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string array to custom object list using linq

Tags:

c#

.net

linq

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.

like image 512
Neeraj Kumar Gupta Avatar asked Mar 25 '13 14:03

Neeraj Kumar Gupta


1 Answers

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();
like image 144
rut0.wut Avatar answered Nov 14 '22 12:11

rut0.wut