reading dictionary key value using linq and storig into a variable

	public static class DictionaryHelper
    {
        public static IDictionary<string, double> LossPercentageDic()
        {
            var dictionaryLossPercentage = new Dictionary<string, double>();
            dictionaryLossPercentage.Add("Less than 10%", 0.05);
            dictionaryLossPercentage.Add("10% to 24%", 0.15);
            dictionaryLossPercentage.Add("25% to 49%", 0.4);
            dictionaryLossPercentage.Add("50% to 74%", 0.66);
            dictionaryLossPercentage.Add("75% and more", 1);

            return dictionaryLossPercentage; 
        }
    }
    
//var Persons1 = values.Where(kvp => PersonList.Contains(kvp.Value))
//      .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

//var Persons = values.Where(kvp => kvp.Value == 0m)
//      .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

//var howMuchAffectedMainString = dictionaryLossPercentage
//		.Where(kvp => kvp.Value == (double)aHousehold.HowMuchAffectedMain)
//      .Select(x => new { Key = x.Key }).ToList().FirstOrDefault().Key;

var howMuchAffectedMainString = aHousehold.HowMuchAffectedMain != null ? 
	DictionaryHelper.LossPercentageDic()
    .Where(kvp => kvp.Value == (double)aHousehold.HowMuchAffectedMain)
    .Select(x => new { Key = x.Key }).ToList().FirstOrDefault().Key : "";
    
    
//dto mapping
HowMuchAffectedMain = aHousehold.HowMuchAffectedMain != null ? 
	DictionaryHelper.LossPercentageDic().Where(kvp => kvp.Value == 
    (double)aHousehold.HowMuchAffectedMain)
    .Select(x => new { Key = x.Key }).ToList().FirstOrDefault().Key : "",

    

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source