converting list of object to array in c# - what does the "x => x.Name" syntax mean?

2.5k views Asked by At

I was looking for a solution to convert each object's properties on a List to some arrays when finally I found this

Then I found this code

MyList.Select(x=>x.Name).ToArray();

which is the solution to my problem. But I still don't understand on what the variable 'x' means. can anyone could explain to me briefly on how this syntax work?

2

There are 2 answers

2
knittl On BEST ANSWER

The LINQ .Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector) extension method expects a Func which takes one argument and returns an object/value of another (or even the same) type.

You have several ways to specify which function to execute for each item in your enumerable:

  • Lambda expressions: what you posted in your question. x => x.Name: x is the input parameter, x.Name is the result. Could also be written as lambda statement: x => { return x.Name; }. Lambda expressions are a very concise way to write short methods which might only be used once. (In addition, they collect their surrounding scope in closures – but this is irrelevant in the context of your question)
  • Anonymous delegate: Mostly the same as above. Would be written as delegate(YourType x) { return x.Name; }
  • Method group: Define a named method somewhere else, and specify its name: .Select(this.SelectName). SelectName would be written as:

    private string SelectName(YourType x) {
      return x.Name;
    }
    

LINQ extension methods generally work on instances of IEnumerable<T>, so any type (mostly collections) which can be iterated/enumerated. .Select() is used to transform one enumerable of type IEnumerable<TSource> to another enumerable of type IEnumerable<TResult>. Note, that the selector function passed as parameter to .Select() is only executed, once the enumerable gets enumerated (by means of foreach, .ToList(), .ToArray(), or others).

With LINQ you focus more on a data centric view of your types. You don't care if .Select() internally uses foreach, for, or any other way to get to your collection's items. It might even parallelize the operation (.AsParallel).

Of course, most LINQ invocations can be rewritten with simple code, for example:

var nameList = List<String>(MyList.Count);
foreach(var item in MyList) {
  nameList.Add(item.Name);
}

var array = nameList.ToArray(); // this is still LINQ,
                                // but I didn't want to bother
                                // with re-allocating the array
                                // or with using indexing
0
Konstantin Dinev On

This is a lambda expression in c#.

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions. To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side. For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.