Skip to main content
added 431 characters in body
Source Link
user33306
user33306

Since you want to return a dictionary, you can simplify things by putting the keys in an array. Now the you can reduce the logic to one line of chained LINQ statements:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    var keys = new string[] { "Summary", "Backlog", "MoreDetails" };
    bool value = false;
    return keys.Select(x => new { key = x, value = dictionary
                                 .TryGetValue(x, out value) ? value : true })
                                 .ToDictionary(x => x.key, x => x.value);
}

If there is a possibility of using a different set of keys you can pass the string array in to the method:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary, string[] keysToCheck)
{
    bool value = false;
    return keysToCheck.Select(x => new { key = x, value = dictionary
                                        .TryGetValue(x, out value) ? value : true })
                                        .ToDictionary(x => x.key, x => x.value);
}

Since you want to return a dictionary, you can simplify things by putting the keys in an array. Now the you can reduce the logic to one line of chained LINQ statements:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    var keys = new string[] { "Summary", "Backlog", "MoreDetails" };
    bool value = false;
    return keys.Select(x => new { key = x, value = dictionary
                                 .TryGetValue(x, out value) ? value : true })
                                 .ToDictionary(x => x.key, x => x.value);
}

Since you want to return a dictionary, you can simplify things by putting the keys in an array. Now the you can reduce the logic to one line of chained LINQ statements:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    var keys = new string[] { "Summary", "Backlog", "MoreDetails" };
    bool value = false;
    return keys.Select(x => new { key = x, value = dictionary
                                 .TryGetValue(x, out value) ? value : true })
                                 .ToDictionary(x => x.key, x => x.value);
}

If there is a possibility of using a different set of keys you can pass the string array in to the method:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary, string[] keysToCheck)
{
    bool value = false;
    return keysToCheck.Select(x => new { key = x, value = dictionary
                                        .TryGetValue(x, out value) ? value : true })
                                        .ToDictionary(x => x.key, x => x.value);
}
Source Link
user33306
user33306

Since you want to return a dictionary, you can simplify things by putting the keys in an array. Now the you can reduce the logic to one line of chained LINQ statements:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    var keys = new string[] { "Summary", "Backlog", "MoreDetails" };
    bool value = false;
    return keys.Select(x => new { key = x, value = dictionary
                                 .TryGetValue(x, out value) ? value : true })
                                 .ToDictionary(x => x.key, x => x.value);
}