\nKey:Column15, Value:\"Jack\"
\nKey:Column16, Value:\"Stevens\"
\nKey:Column18, Value:\"7/23/1973\"
\nKey:Column25, Value:\"Active\"
Hashtable2:
\nKey:Column15, Value:\"Melanie\"
\nKey:Column16, Value:\"Teal\"
\nKey:Column18, Value:\"null\"
\nKey:Column25, Value:\"Inactive\"
Hashtable3:
\nKey:Column15, Value:\"Henry\"
\nKey:Column16, Value:\"Black\"
\nKey:Column18, Value:\"3/16/1913\"
\nKey:Column25, Value:\"Active\"
Use of a static type instead of a Hashtable is out of the question because the result of the query is unknown at run time; both the number of columns and the nature of those columns is completely dynamic.
\n\nI'd like to be able to perform Linq based operations on this data set (grouping, ordering etc), but I absolutely can't get my head around what the syntax might look like. As a simple example, let's say I want to sort the list by Column15 descending. The best syntax I've come up with is:
\n\nvar rawGridData = (List<Hashtable>) _listDao.GetListGridContents(listID, null, null); \nvar sortedGridData = rawGridData.OrderBy(s => s.Keys.Cast<string>().Where(k => k == \"Column15\")); \n
\n\nHowever, this yields an exception when sortedGridData is enumerated: \"At least one object must implement IComparable.\"
\n\nI've been struggling with this problem for days and am near my wit's end...please help!
\n","author":{"@type":"Person","name":"Mitch A"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"This should get you started:
\n\nvar sortedGridData = rawGridData.OrderBy(r => r[\"Column15\"])\n
\n\nThis maps each \"record\" to the value in \"Column15\" and then orders the resulting projection. This is easily generalizable.
\n","author":{"@type":"Person","name":"jason"},"upvoteCount":2}}}Reputation: 2118
I'm working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/hashtables:
Hashtable1:
Key:Column15, Value:"Jack"
Key:Column16, Value:"Stevens"
Key:Column18, Value:"7/23/1973"
Key:Column25, Value:"Active"
Hashtable2:
Key:Column15, Value:"Melanie"
Key:Column16, Value:"Teal"
Key:Column18, Value:"null"
Key:Column25, Value:"Inactive"
Hashtable3:
Key:Column15, Value:"Henry"
Key:Column16, Value:"Black"
Key:Column18, Value:"3/16/1913"
Key:Column25, Value:"Active"
Use of a static type instead of a Hashtable is out of the question because the result of the query is unknown at run time; both the number of columns and the nature of those columns is completely dynamic.
I'd like to be able to perform Linq based operations on this data set (grouping, ordering etc), but I absolutely can't get my head around what the syntax might look like. As a simple example, let's say I want to sort the list by Column15 descending. The best syntax I've come up with is:
var rawGridData = (List<Hashtable>) _listDao.GetListGridContents(listID, null, null);
var sortedGridData = rawGridData.OrderBy(s => s.Keys.Cast<string>().Where(k => k == "Column15"));
However, this yields an exception when sortedGridData is enumerated: "At least one object must implement IComparable."
I've been struggling with this problem for days and am near my wit's end...please help!
Upvotes: 0
Views: 1429
Reputation: 241779
This should get you started:
var sortedGridData = rawGridData.OrderBy(r => r["Column15"])
This maps each "record" to the value in "Column15" and then orders the resulting projection. This is easily generalizable.
Upvotes: 2