You can use the Cast()
extension method to cast objects in a collection to a specific type:
var secondaryList = primaryList.Select(x => x.Value)
.Cast<Dictionary<string, object>>();
This will fail with an exception if any of the objects in the collection are not cast-able to Dictionary<string, object>
. You can also use OfType<Dictionary<string, object>>()
to select only those elements which are of that particular type.
var secondaryList = primaryList.Select(x => x.Value)
.OfType<Dictionary<string, object>>();
When selecting a subset of a dictionary, you can use the ToDctionary()
extension method.
Dictionary<string, object> l_d = new Dictionary<string, object>();
l_d.Add( "Apple", 1 );
l_d.Add( "Access", 2 );
l_d.Add( "Barber", 3 );
l_d.Add( "Bacon", 4 );
Dictionary<string, object> l_d2 =
l_d.Where( x => x.Key.StartsWith( "A" ) )
.ToDictionary( kvp => kvp.Key, kvp => kvp.Value );