Reputation: 97
I would like to order all elements
This is the way I can iterate through it:
List<Dictionary<string, object>> valueList = ((IEnumerable<object>)y.Value).Select(x => (Dictionary<string, object>)x).ToList();
foreach (Dictionary<string, object> dict in valueList)
{
foreach (KeyValuePair<string, object> item in dict)
{
}
}
I have huge problems with forming a LINQ-expression, which will order all the values to a specific key.(For example, I have special key and that value I want to reorder the entire data source)
valueList.OrderBy(ya => (ya.Values as List<Dictionary<string, KeyValuePair<string, object>>>).Keys.First(key => key.Equals("propertyToSearchFor")));
I get:
Cannot convert type 'System.Collections.Generic.Dictionary.ValueCollection' to 'System.Collections.Generic.List>>' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
What should it be instead ?
UPDATED 1
Thanks for answering, this is an example of the data I use http://i63.tinypic.com/2d0mhb9.png .
I have my valueList
, which I need to reorder, depending on the key "propertyToSearchFor"(take a look at the screenshot: "modell","marke" or ..., "jan"). For example valueList[0]
contains some kind of data set, which has the same key's in valueList[1]
, but the value in valueList[1]
are different than in valueList[0]
.
I need to order the data resource by "modell" it should go through all elements in valueList[...]
and reorder that list depending on the value of modell.
UPDATED 2
Here is something for copy and paste :)
List<Dictionary<string, object>> valueList = new List<Dictionary<string, object>>();
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 34.0f},
{"property5", 5.0d},
{"property6", 'c'},
{"property7", "xtest"},
{"property8", "gtest"},
{"property9", "jtest"},
{"property10", "1ptest"},
{"property11", "atest"},
{"property12", "test"},
{"property13", "ätest"},
{"property14", "test"},
{"property15", "ztest"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 342.0f},
{"property5", 25.0d},
{"property6", 'h'},
{"property7", "1xtest"},
{"property8", "gtest"},
{"property9", "1jtest"},
{"property10", "1ptest"},
{"property11", "atest"},
{"property12", "1test"},
{"property13", "1ätest"},
{"property14", "test"},
{"property15", "ztest"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 344.0f},
{"property5", 5.0d},
{"property6", 'z'},
{"property7", "xtest"},
{"property8", "gt213est"},
{"property9", "jtest"},
{"property10", "2311ptest"},
{"property11", "21atest"},
{"property12", "321test"},
{"property13", "231ätest"},
{"property14", "31test"},
{"property15", "z231test"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 3.0f},
{"property5", 500.0d},
{"property6", 'z'},
{"property7", "xtest"},
{"property8", "gstest"},
{"property9", "jtest"},
{"property10", "1pstest"},
{"property11", "atsest"},
{"property12", "test"},
{"property13", "ätsest"},
{"property14", "tesst"},
{"property15", "ztsest"},
});
Upvotes: 0
Views: 1311
Reputation:
Are you looking for the OfType
method?
valueList.OrderBy(ya => ya.Values.OfType<Dictionary<string,object>>().First(key => key.Equals("propertyToSearchFor")));
After your update2
var test = valueList.Select(x => new { a=x, b=x["property4"] })
.OrderByDescending(x => x.b).Select(x=>x.a).ToList();
in case you want to manage a non existing key (to avoid the exception)
Func<string,Dictionary<string,object>,object> func = (s,x) => { object o = null; x.TryGetValue(s, out o); return o; };
var test = valueList.Select(x => new { a=x, b = func("nonExisting",x)})
.OrderByDescending(x => x.b).Select(x=>x.a).ToList();
So the following is ordering your valueList
in update2 (descending) by an (existing) property5
var testProperty5Desc = valueList.Select(x => new { a=x, b = func("property5",x)})
.OrderByDescending(x => x.b).Select(x=>x.a).ToList();
Upvotes: 1
Reputation: 1
In the example, the variable ya
is an element of valueList
.
So it's a Dictionary<string, object>
type.
So this line of code should work.
valueList.OrderBy(ya => ya.Values.First(key => key.Equals("propertyToSearchFor")));
Upvotes: 0
Reputation: 37000
if valueList
is a List<T>
with T
being a Dictionary<string, object>
there is no member Values
, or am I missing anything? The current element ya
already is a Dictionary<string, object>
, so you won´t need to cast it:
List<Dictionary<string, object>> valueList = ...
var result = valueList.OrderBy(ya => ya.Keys.First(key => key.Equals("propertyToSearchFor")));
Upvotes: 0