reading dictionary key value using linq and storig into a variable code example

Example: 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 : "",

Tags:

Misc Example