TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Select Method (Use Lambda to Modify Elements)

Use the Select extension from the System.Linq namespace. Select calls a lambda to modify each element.
Select. This method applies a method to elements. It is an elegant way to modify the elements in a collection such as an array. This method receives as a parameter an anonymous function—typically specified as a lambda expression.LINQ
Example. Let's look at a program where the Select extension method is applied to a string array. A local variable of array type is allocated and three string literals are used. We use Select on this array reference.String ArrayString Literal

Then: The Select method specifies a lambda expression, which applies the string instance method ToUpper to each element in the array.

Uppercase: Each string element is modified to be its uppercase representation. The result of ToUpper is used.

ToLower

Finally: We use the foreach-loop. And the Console.WriteLine method prints the results to the screen.

ForeachConsole
C# program that uses Select method using System; using System.Linq; class Program { static void Main() { // An input data array. string[] array = { "cat", "dog", "mouse" }; // Apply a transformation lambda expression to each element. // ... The Select method changes each element in the result. var result = array.Select(element => element.ToUpper()); // Display the result. foreach (string value in result) { Console.WriteLine(value); } } } Output CAT DOG MOUSE
The Select method can definitely be used on many different collection types, not just an array or a string type array. You can experiment with it on List types, and other array types, and even results from other query expressions.
Lambda expression syntax is somewhat tricky. The "=>" in this context does not specify a comparison, but rather can be read as "goes to." It separates the arguments from the body of the method.

Tip: A lambda expression is the same as a regular method conceptually, but written in a more condensed syntax.

Overload. The Select extension method also has an overload that receives a different form of anonymous mutator function as the argument. This version allows you to use the index of the element inside the body of the lambda expression.

And: This gives you the ability to apply the index to the result of the Select method and its mutation effects.

Overload Method
Summary. We looked at the simplest form of the Select extension method from the System.Linq namespace in the C# language. The name "select" is possibly confusing, as the method actually provides a mutation function, not just a selection function.SelectMany
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf