Can I initialize an object in one statement using lambda for each syntax?

If I have these two classes:

public class StudyClass
{
    public string className { get; set; }
    public List<Student> students { get; set; }
}

public class Student
{
    public string studentName { get; set; }
}

Then I can initialize the StudyClass object like that:

var classObject = GetClassData(); // returns a big object with many properties that I don't need

var studyClass= new StudyClass() {
    className = classObject.className
}

foreach(var student in classObject.students)
{
    studyClass.students.add(new Student() {
        studentName = student.Name
    });
}

Is it possible to do it in a more simple way, by doing something like:

var classObject = GetClassData(); // returns a big object with many properties that I don't need

var studyClass= new StudyClass() {
    className = classObject.className,
    students = classObject.students.ForEach...// i am stuck here
}

If it's possible, is there any performance benefit or drawback by doing that ?

Answers


Yes, you can do this using the LINQ Select method followed by returning the results as a list using ToList:

var classObject = GetClassData();

var studyClass = new StudyClass {
    className = classObject.className
    students = classObject.students.Select(s => new Student { studentName = s.Name}).ToList()
};

This will enumerate classObject.students calling the lambda function once for each one, where the expression returns a new Student using the current value (s) to set the studentName property.

is there any performance benefit or drawback by doing that ?

It's unlikely to be any more performant; internally it still has to enumerate classObject.students and has to call the lambda method, while the original method has to make use of List.Add. You need to properly measure the timings to find out if it makes a worthwhile difference in your environment.


You can do it using linq:

var studyClass= new StudyClass() {
    className = classObject.className,
    students = classObject.students.Select(s => new Student { studentName = s.Name }).ToList();
}

There maybe a little performance improvement. You have to enumerate classObject.students in both variations, but Select and ToList may be faster than calling List.Add for each single student.


You can just project a list using Select() and ToList(), like this:

var studyClass= new StudyClass() 
{
   className = classObject.className,
   students = classObject.students.Select(s=>new Student(){Name=s.Name}).ToList()
}

It looks like you are wanting to clone the students from one list and append them to another list.

You can do that like this:

studyClass.students.AddRange(
    classObject.students.Select(student => 
        new Student {studentName = student.studentName}));

If you actually want to replace the list with a new one, you can do this instead:

studyClass.students = new List<Student>(
    classObject.students.Select(student => 
        new Student {studentName = student.studentName}));

This is likely to be marginally more performant than other methods, but the difference will probably be so small as to be negligible.


Need Your Help

Will the Order of my Associative Array be maintained from PHP to Javascript?

php javascript ajax json

In PHP I'm running a mysql_query that has an ORDER BY clause. I'm then iterating through the results to build an associative array, with the row_id as the key.

.gitignore does not understand my folder wildcard on windows

windows git msysgit gitignore

I'm encountering a weird issue with .gitignore on Windows.