3

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 ?

3
  • A little remark on yours bracing style - in C# it is more common practice to open and close braces on a new line (Allman style). And the style you are using is more like JS bracing style. The reason why JS uses such a style is well described here. In C# tt really doesn't affect anything, but is much simpier to read. VS editor's default formatting can rearrange all the braces for you - just press Ctrl + K, D. Commented Jan 21, 2016 at 10:17
  • @James Actually, no, Allman style makes no exuses for any cases. Even one line if statement body should be surrounded by curly braces placed on the new line. Try using FormatDocument option in VS with default C# settings - it will move brace into a new line. Commented Jan 21, 2016 at 10:23
  • @Yura Thanks for the remark. I normally format the braces properly, I am sorry that I haven't done it here. I've been dealing with Android and Java lately.. Commented Jan 21, 2016 at 12:36

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the explanation. So it's basically better to use the other approach.
Not necessarily - this one is (arguably) more readable - you'd have to do some proper performance testing to find out which is actually faster, and if it's even relevant that one is faster than the other.
You have to enumerate in both variations, but ToList maybe faster than a List.Add for each student. But of course you should think about if this performance difference does matter at all.
@RenéVogt That's true - I'd forgotten about the original call to .Add. Still needs measuring to find out if it's worth worrying about :)
Note that this isn't quite the same as the OP. In the OP, the list studyClass.students has all the new students appended to it, which will retain any students that were already in it. This answer will replace any existing students with the new ones. This is likely what the OP intended anyway, but it's worth noting the difference.
|
1

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.

Comments

1

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()
}

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.