7

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.

2
  • 4
    Well, 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. Commented Mar 25, 2013 at 14:05
  • You can do it exactly how you said, using LINQ. Everything you need can be found with some effort on your side. We won't code it for you. See this: msdn.microsoft.com/en-us/library/bb397933.aspx Commented Mar 25, 2013 at 14:12

1 Answer 1

19

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();
Sign up to request clarification or add additional context in comments.

1 Comment

wouldn't it be sufficient enough to use just: List<CustomEntity> = randomArray.ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.