Data transforms using LINQ

Here’s som simple code to transform an array or list from one type to another:

Kursdeltagare[] kdArr = ...

// Alternative 1
var kdDtos = matchingKd.Select(kd => new KursdeltagareDto()
                                    {
                                        PersonId = kd.PersonID,
                                        Fornamn = kd.Fornamn,
                                        Efternamn = kd.Efternamn
                                    }).ToList();

// Alternative 2
var kdDtos2 = (from kd in matchingKd
             select new KursdeltagareDto()
                        {
                            PersonId = kd.PersonID,
                            Fornamn = kd.Fornamn,
                            Efternamn = kd.Efternamn
                        }
            ).ToList();

Both alternatives will give the same result but the first one contains slightly less code. Which is better from a performance point of view? If you know, feel free to leave a comment…

/Emil

1 thought on “Data transforms using LINQ”

  1. I believe those are exactly the same. Linq queries are converted to calls to lambda extension methods by the compiler. I guess that if you looked at the IL code generated by both alternatives, it would probably be exactly the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.