c# - Linq for "List<Dictionary<string, KeyValuePair<string, object>>>" order the values by asc -
i order elements
this way 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 forming linq-expression, order values specific key.(for example, have special key , value want reorder entire data source)
valuelist.orderby(ya => (ya.values list<dictionary<string, keyvaluepair<string, object>>>).keys.first(key => key.equals("propertytosearchfor")));
i get:
cannot convert type 'system.collections.generic.dictionary.valuecollection' 'system.collections.generic.list>>' via reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
what should instead ?
updated 1
thanks answering, example of data use http://i63.tinypic.com/2d0mhb9.png .
i have valuelist
, need reorder, depending on key "propertytosearchfor"(take @ screenshot: "modell","marke" or ..., "jan"). example valuelist[0]
contains kind of data set, has same key's in valuelist[1]
, value in valuelist[1]
different in valuelist[0]
.
i need order data resource "modell" should go through elements in valuelist[...]
, reorder list depending on value of modell.
updated 2
here copy , 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"}, });
are looking oftype
method?
valuelist.orderby(ya => ya.values.oftype<dictionary<string,object>>().first(key => key.equals("propertytosearchfor")));
after update2
var test = valuelist.select(x => new { a=x, b=x["property4"] }) .orderbydescending(x => x.b).select(x=>x.a).tolist();
in case want manage non existing key (to avoid 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 following ordering valuelist
in update2 (descending) (existing) property5
var testproperty5desc = valuelist.select(x => new { a=x, b = func("property5",x)}) .orderbydescending(x => x.b).select(x=>x.a).tolist();
Comments
Post a Comment