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# ToLookup Method (Get ILookup)

Use the ToLookup extension method from System.Linq. ToLookup gets an ILookup.
ToLookup returns a data structure that allows indexing. It is an extension method. We get an ILookup instance that can be indexed or enumerated using a foreach-loop. The entries are combined into groupings at each key.LINQ
First, this example uses an input string array of 3 string elements. The ToLookup method receives a Func delegate. This is specified as a lambda expression. The implicit type syntax (var) is used on the result of the ToLookup method.Var

Then: The key with value 3 and key with value 5 are accessed. The groupings are looped over. All groupings are accessed and enumerated.

Note: The lambda expression in this example specifies that the lookup value for each string is the string's length.

String Length

Therefore: With this lambda expression, a string such as "cat" will have a lookup value of 3.

C# program that uses ToLookup method using System; using System.Linq; class Program { static void Main() { // Create an array of strings. string[] array = { "cat", "dog", "horse" }; // Generate a lookup structure, // ... where the lookup is based on the key length. var lookup = array.ToLookup(item => item.Length); // Enumerate strings of length 3. foreach (string item in lookup[3]) { Console.WriteLine("3 = " + item); } // Enumerate strings of length 5. foreach (string item in lookup[5]) { Console.WriteLine("5 = " + item); } // Enumerate groupings. foreach (var grouping in lookup) { Console.WriteLine("Grouping:"); foreach (string item in grouping) { Console.WriteLine(item); } } } } Output 3 = cat 3 = dog 5 = horse Grouping: cat dog Grouping: horse
In the C# language, a lambda expression is a procedure specified in a different syntax. The formal parameters are specified on the left side of the => token, while the method body is specified on the right side.Lambda
ToDictionary. The Dictionary data structure requires that only one key be associated with one value. The ToLookup data structure, however, is more lenient. It allows one key to be associated with multiple values.ToDictionary
Summary. We saw the ToLookup extension method in the System.Linq namespace. This method is useful when you want to create a Dictionary-type data structure using LINQ but do not want to enforce a single key-value mapping strategy.
© 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