I would like to know if there another way to filter dictionnary
public class location
{
public Dictionary<int, Dictionary<int, Dictionary<int, location>>> Example{ get; set; }
}
public class location
{
public int Id { get; set; }
public string Name{ get; set; }
}
i would like to filter dictionary where Id = 3
I need to filter the Example dictionary where the Id property of LocationBO equals 3. Currently, I achieve this with the following code:
var filteredLocations = locations.Select(location =>
{
var filteredDict = location
.Example
.ToDictionary(
outerKey => outerKey.Key,
outerValue => outerValue.Value
.ToDictionary(
middleKey => middleKey.Key,
middleValue => middleValue.Value
.Where(innerEntry => innerEntry.Value.id== 3)
.ToDictionary(innerKey => innerKey.Key, innerValue => innerValue.Value)
)
);
location.Example= filteredDict;
return new Location(location);
})
.ToList();
Is there a more efficient or readable way to filter this nested dictionary structure based on the Id property?