I have an enumerable as follows:
IEnumerable<dynamic> collection = new
{
column1 = "1", column2 = "name", column3= "somevalue"
},
new
{
column1 = "2", column2 = "name2", column3= "somevalue2"
},
new
{
column1 = "3", column2 = "name3", column3= "somevalue3"
}
and so on….The number of columns in the dynamic types can also vary.No of columns can be more or less.
Now if I want to search this IEnumerable collection for e.g. somevalue2, I need the following information
Found in Row 2 (Assume column1 is always primary key) in Column “column2”.
Which would be the best way to translate this dynamic collection into a data structure (.NET List/Collection/Hashtable etc.) and the best way to query the information from the collection. The collection will at most will be a page of data (25 rows).
Any code snippets and data structures to do a super fast efficient way of searching the data would be appreciated. Assume the search term is “contains” match as opposed to a perfect match.
Those are anonymous types; anonymous types are a compiler feature, so that layout is fixed rigid at compile time, so they aren’t going to vary. I would suggest: don’t use
dynamic. For example:which is all strongly typed and well-defined. You can use regular operations, for example:
again, all static-typed.
If you want to search all the columns, then I would suggest something like a dictionary is better:
Since you can then just look at
.Values.Contains(...)Edit: with the comment I think I understand it more clearly; I am picturing an opaque method that returns
IEnumerable<dynamic>that are actually POCO types (not dynamic types) and we want to check the string members (arbitrary names) for a match. We should be able to do that with reflection, but FastMember will make it much faster: