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

  • SEARCH
  • Home
  • 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 7164285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:03:02+00:00 2026-05-28T14:03:02+00:00

I have a generic Dictionary where TValue is of type String ( Dictionary<int, string>

  • 0

I have a generic Dictionary where TValue is of type String (Dictionary<int, string>). I chose to use string as the value type because the data was loaded from an Xml file where the source values can be character or numeric data types (I suppose Object would’ve been an acceptable TValue type too, but even then this question would be equally applicable).

The character data types also have importance, so excluding them outright isn’t an option.

I’d like to extract a subset of this Dictionary<int, double>. In other words, I’d like the subset of the dictionary where the values are numeric.

Right now I’m doing it like this:

Dictionary<int, string> myDictionary;
// Do some loading.
var numericData = myDictionary.Where(kvp => Double.TryParse(kvp.Value, out temp)       

This approach is awfully ugly and doesn’t get me the result as a Dictionary<int, double> Can anyone offer other ways to improve this?

Thanks!

  • 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-28T14:03:02+00:00Added an answer on May 28, 2026 at 2:03 pm

    The code you’ve given is not only ugly – it will fail with an InvalidCastException at execution time. I suspect you actually want:

    var numericData = myDictionary
            .Select(kvp => {
                        double value;
                        return new { kvp.Key,
                            Value = double.TryParse(kvp.Value, out value) 
                                       ? value : (double?) null
                        };
                   })
            .Where(pair => pair.Value != null)
            .ToDictionary(pair => pair.Key, pair => pair.Value.Value);
    

    And yes, that’s ugly – but:

    • It avoids parsing the value more than once
    • It avoids putting side-effects in your query

    You can make it slightly cleaner but less efficient if you’re happy to parse twice:

    var numericData = myDictionary
           .Where(kvp => { double tmp; return double.TryParse(kvp.Value, out tmp); })
           .ToDictionary(pair => pair.Key, pair => double.Parse(pair.Value));
    

    Or (more cleanly) you could create a separate method:

    public static double? TryParseNullableDouble(string text)
    {
        double value;
        return double.TryParse(text, out value) ? value : (double?) null;
    }
    

    Then the first version becomes:

    var numericData = myDictionary
            .Select(kvp => new { kvp.Key, TryParseNullableDouble(kvp.Value) })
            .Where(pair => pair.Value != null)
            .ToDictionary(pair => pair.Key, pair => pair.Value.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a System.Collections.Generic.Dictionary<string, string> containing control ID and appropriate data column to data
Looking at System.Collections.Generic.Dictionary<TKey, TValue> , it clearly implements ICollection<KeyValuePair<TKey, TValue>> , but doesn't have
Say that i have a generic dictionary with data like this (I hope the
I have a class that inherits from a generic dictionary as follows: Class myClass
I have a System.Collections.Generic.Dictionary<System.Web.UI.Control, object> where all keys can be either type of System.Web.UI.WebControls.HyperLink
I get the error Using the generic type 'System.Collections.Generic.Dictionary< TKey, TValue>' requires 2 type
I have a Dictionary data base and I want to return an element from
I'm using a System.Collections.Generic.Dictionary<string, string> . I want to return the first key from
I have a WCF application, written in VB.NET that takes a generic Dictionary(Of String,
There's an XML scheme which says something like this: <ExtraFields> <ExtraField Type=Int> <Key>Mileage</Key> <Value>500000

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.