Understanding Dictionaries, Lambda, and LINQ: Associative Arrays, Expressions, and Queries, Study Guides, Projects, Research of Programming Languages

An introduction to associative arrays using Dictionaries and SortedDictionaries in C#, as well as an overview of Lambda expressions and LINQ queries for filtering, mapping, and ordering collections. Learn how to use these powerful features for processing sequences and manipulating data structures.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 03/04/2022

nguyen-ly-6
nguyen-ly-6 🇻🇳

4.8

(10)

43 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dictionaries, Lambda and
LINQ
Collections and Queries
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Understanding Dictionaries, Lambda, and LINQ: Associative Arrays, Expressions, and Queries and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

Dictionaries, Lambda and

LINQ

Collections and Queries

  • Associative Arrays
    • Dictionary <key, value>
    • SortedDictionary <key, value>
  • Lambda
  • LINQ
    • Filtering
    • Mapping
    • Ordering Table of Contents 2
  • Associative arrays are arrays indexed by keys
    • Not by the numbers 0, 1, 2, … (like arrays)
  • Hold a set of pairs {key → value} Associative Arrays (Maps, Dictionaries) 4 John Smith +1- 555 - 8976 Lisa Smith +1- 555 - 1234 Sam Doe +1- 555 - 5030 Key Value
  • Dictionary<K, V> - collection of key and value pairs
  • Keys are unique
  • Keeps the keys in their order of addition
  • Uses a hash-table + list Dictionary var fruits = new SortedDictionary<string, double>(); fruits["kiwi"] = 4.50; fruits["orange"] = 2.50; fruits["banana"] = 2.20;

Add ( key , value ) method ▪ Remove(key ) method Built-In Methods var airplanes = new Dictionary<string, int>(); airplanes.Add("Boeing 737", 130); airplanes.Add("Airbus A320", 150); var airplanes = new Dictionary<string, int>(); airplanes.Add("Boeing 737", 130); airplanes.Remove("Boeing 737");

ContainsKey(key)ContainsValue(value) Built-In Methods (2) var dictionary = new Dictionary<string, int>(); dictionary.Add("Airbus A320", 150); if (dictionary.ContainsKey("Airbus A320")) Console.WriteLine($"Airbus A320 key exists"); var dictionary = new Dictionary<string, int>(); dictionary.Add("Airbus A320", 150); Console.WriteLine(airplanes.ContainsValue(150)); //true Console.WriteLine(airplanes.ContainsValue(100)); //false

Traditional Dictionary: Add() 10 Dictionary<string, string> Key Value Pesho 0881 - 123 - 987 Gosho 0881 - 123 - 789 Alice 0881 - 123 - (^978) Hash Function

Dictionary: Remove() 11 Dictionary<string, string> Key Value Hash Function Pesho Pesho 0881 - 123 - 987 Gosho 0881 - 123 - 789 Alice 0881 - 123 - 978

  • Using foreach loop
  • Iterate through objects of type KeyValuePair<K, V>
  • Cannot modify the dictionary (read-only) Iterating through Dictionary var fruits = new Dictionary<string, double>(); fruits.Add("banana", 2.20); fruits.Add("kiwi", 4.50); foreach (var fruit in fruits) Console.WriteLine($"{fruit.Key} - > {fruit.Value}"); fruit.Key - > fruit name fruit.Value - > fruit price
  • Read 2 * N lines of pairs word and synonym
  • Each word may have many synonyms Problem: Word Synonyms 14 3 cute adorable cute charming smart clever cute - adorable, charming smart - clever

LAMBDA EXPRESSIONS

Anonymous Functions

  • A lambda expression is an anonymous function containing

expressions and statements

  • Lambda expressions
  • Use the lambda operator =>
    • Read as "goes to"
  • The left side specifies the input parameters
  • The right side holds the expression or statement Lambda Functions a => a > 5; 17
  • Min() – finds the smallest element in a collection
  • Max() – finds the largest element in a collection
  • Sum() – finds the sum of all elements in a collection
  • Average() – finds the average of all elements in a collection Processing Sequences with LINQ 19 new List() { 1, 2, 3, 4, - 1, - 5, 0, 50 }.Min() //- 5 new int[] { 1, 2, 3, 40, - 1, - 5, 0, 5 }.Max() // 40 new long[] {1, 2, 3, 4, - 1, - 5, 0, 50}.Sum() // 54 new int[] {1, 2, 3, 4, - 1, - 5, 0, 50}.Average() //6.
  • Select() manipulates elements in a collection Manipulating Collections 20 string[] words = { "abc", "def" } ; var result = words.Select(w => w + "x"); // words - > abcx, defx var nums = Console.ReadLine() .Split() .Select(int.Parse);