Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to filter a map in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var myDictionary = new Dictionary<int, string>
        {
            { 1, "C#" },
            { 2, "C" },
            { 3, "C++" },
            { 4, "Java" },
            { 5, "Python" }
        };

        // Filter: Keep only entries where the key is greater than 2
        var filteredDictionary = myDictionary
            .Where(kv => kv.Key > 2)
            .ToDictionary(kv => kv.Key, kv => kv.Value);

        foreach (var kv in filteredDictionary) {
            Console.WriteLine($"Key: {kv.Key}, Value: {kv.Value}");
        }
    }
}



/*
run:

Key: 3, Value: C++
Key: 4, Value: Java
Key: 5, Value: Python

*/

 



answered Aug 6, 2025 by avibootz

Related questions

1 answer 53 views
1 answer 120 views
3 answers 81 views
3 answers 77 views
1 answer 60 views
60 views asked Aug 7, 2025 by avibootz
...