Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:07:49+00:00 2026-05-23T11:07:49+00:00

I have a Dictionary<int int> . When I check the keys of the Dictionary

  • 0

I have a Dictionary<int int>. When I check the keys of the Dictionary for a number and it is in it, I want it to return the number, else I want the linq query to return 0.

Something like the following, except working

var t = (from result in results
         where result.Key == 3
         select result.Key != null ? result.Value : 0).First();

Because the problem is that when there is no number in the list, the sequence contains no element, so you can’t check with a null or a count.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:07:50+00:00Added an answer on May 23, 2026 at 11:07 am

    Just use TryGetValue.

    int i;
    results.TryGetValue(3, out i);
    

    If it finds it, i is set to the value. If not, i is default, which will be zero for int.

    If you want another value besides default, you can do this:

    int i;
    if (!results.TryGetValue(3, out i))
    {
        i = 5; // or whatever other value you want;
    }
    

    And if you, like me, hate the out parameter style, you can create an extension method

    public static class IDictionaryExtensions
    {
        public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
        {
            T i;
            dictionary.TryGetValue(key, out i);
            return i;
        }
    }
    

    and then you can call:

    int i = dictionary.GetValueOrDefault(3);
    

    and if you want to get fancier you can create another oveload of the extension:

        public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
        {
            T i;
            return dictionary.TryGetValue(key, out i) ? i : defaultValue;
        }
    

    which can be called

    int i = dictionary.GetValueOrDefault(3, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary of type Dictionary<Guid,int> I want to return the first instance
I have a Dictionary<int, string> which I want to take the Key collection into
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have a dictionary declared like this. public static Dictionary<int?, List<int?>> pegMap = new
I have a dictionary that has int keys. The keys are in random order
I have a dictionary of type Dictionary<int, double> and want to eliminate values equal
assume we have : class Dictionary { int n; int *ints; char **strs; inline
I have Two Dictionary DictA & DictB such as: Dictionary<int, string> DictA = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.