Dictionary search with Linq

Dictionary search with Linq

In C#, you can perform a search on a dictionary using LINQ queries to find specific key-value pairs that match certain conditions. The LINQ extension methods allow you to filter and retrieve data from the dictionary based on your search criteria.

Assuming you have a Dictionary<TKey, TValue> and you want to find key-value pairs that meet specific conditions, here are some examples using LINQ:

using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { // Sample dictionary Dictionary<int, string> myDictionary = new Dictionary<int, string> { { 1, "Apple" }, { 2, "Banana" }, { 3, "Orange" }, { 4, "Grapes" }, { 5, "Mango" } }; // Example 1: Find key-value pairs where the key is even var evenKeys = myDictionary.Where(pair => pair.Key % 2 == 0); // Example 2: Find key-value pairs where the value contains the letter "a" (case-insensitive) var aInValue = myDictionary.Where(pair => pair.Value.IndexOf("a", StringComparison.OrdinalIgnoreCase) >= 0); // Example 3: Find the value associated with a specific key (if it exists) int searchKey = 3; var searchedValue = myDictionary.TryGetValue(searchKey, out string value) ? value : "Not found"; // Example 4: Find key-value pairs where the value length is greater than 5 characters var longValues = myDictionary.Where(pair => pair.Value.Length > 5); // Print the results Console.WriteLine("Even keys:"); foreach (var pair in evenKeys) { Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}"); } Console.WriteLine("\nValues containing 'a':"); foreach (var pair in aInValue) { Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}"); } Console.WriteLine($"\nValue associated with key {searchKey}: {searchedValue}"); Console.WriteLine("\nValues with length greater than 5 characters:"); foreach (var pair in longValues) { Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}"); } } } 

Output:

Even keys: Key: 2, Value: Banana Key: 4, Value: Grapes Values containing 'a': Key: 1, Value: Apple Key: 2, Value: Banana Key: 4, Value: Grapes Value associated with key 3: Orange Values with length greater than 5 characters: Key: 4, Value: Grapes 

In the examples above, we used various LINQ extension methods like Where() and TryGetValue() to perform the searches on the dictionary and retrieve the desired key-value pairs or values. Remember that LINQ queries return IEnumerable<T> collections, which you can iterate through or perform further operations as needed.

Examples

  1. C# Dictionary Search with LINQ

    • Description: Demonstrates how to search for a specific key-value pair in a dictionary using LINQ in C#.
    • Code Implementation:
      // Search for a key-value pair using LINQ var searchResult = myDictionary.FirstOrDefault(kv => kv.Key == "SearchKey"); 
  2. C# Dictionary Contains Key with LINQ

    • Description: Illustrates how to check if a dictionary contains a specific key using LINQ in C#.
    • Code Implementation:
      // Check if a dictionary contains a key using LINQ bool containsKey = myDictionary.Any(kv => kv.Key == "SearchKey"); 
  3. C# Dictionary Contains Value with LINQ

    • Description: Shows how to check if a dictionary contains a specific value using LINQ in C#.
    • Code Implementation:
      // Check if a dictionary contains a value using LINQ bool containsValue = myDictionary.Any(kv => kv.Value == "SearchValue"); 
  4. C# Dictionary Search for Key with LINQ SingleOrDefault

    • Description: Demonstrates searching for a key-value pair in a dictionary using SingleOrDefault with LINQ in C#.
    • Code Implementation:
      // Search for a key-value pair using LINQ and SingleOrDefault var searchResult = myDictionary.SingleOrDefault(kv => kv.Key == "SearchKey"); 
  5. C# Dictionary Search for Key and Value with LINQ

    • Description: Illustrates how to search for a key-value pair in a dictionary using LINQ based on both key and value.
    • Code Implementation:
      // Search for a key-value pair using LINQ with key and value criteria var searchResult = myDictionary.FirstOrDefault(kv => kv.Key == "SearchKey" && kv.Value == "SearchValue"); 
  6. C# Dictionary Key Exists with LINQ

    • Description: Shows how to check if a specific key exists in a dictionary using LINQ in C#.
    • Code Implementation:
      // Check if a specific key exists in a dictionary using LINQ bool keyExists = myDictionary.Keys.Any(k => k == "SearchKey"); 
  7. C# Dictionary Search for Values Matching Criteria with LINQ

    • Description: Demonstrates searching for values in a dictionary that match specific criteria using LINQ in C#.
    • Code Implementation:
      // Search for values in a dictionary matching a specific criteria using LINQ var searchResults = myDictionary.Where(kv => kv.Value.StartsWith("SearchPrefix")).ToList(); 
  8. C# Dictionary Search for Key and Value with LINQ Single

    • Description: Illustrates searching for a key-value pair in a dictionary using Single with LINQ in C#.
    • Code Implementation:
      // Search for a key-value pair using LINQ with Single var searchResult = myDictionary.Single(kv => kv.Key == "SearchKey" && kv.Value == "SearchValue"); 
  9. C# Dictionary Search with LINQ Case-Insensitive Key

    • Description: Demonstrates searching for a key in a dictionary with a case-insensitive comparison using LINQ in C#.
    • Code Implementation:
      // Search for a key with case-insensitive comparison using LINQ var searchResult = myDictionary.FirstOrDefault(kv => kv.Key.Equals("searchkey", StringComparison.OrdinalIgnoreCase)); 
  10. C# Dictionary Search with LINQ Projection

    • Description: Shows how to use LINQ to project specific properties from a dictionary based on search criteria.
    • Code Implementation:
      // Search and project specific properties from a dictionary using LINQ var searchResults = myDictionary.Where(kv => kv.Key.StartsWith("SearchPrefix")).Select(kv => new { kv.Key, kv.Value }).ToList(); 

More Tags

stub roblox cisco excel-2013 dx excel-2010 node-postgres google-sheets jenkins-scriptler comdlg32

More C# Questions

More Other animals Calculators

More Chemistry Calculators

More Fitness Calculators

More General chemistry Calculators