I have a method that returns List<IDictionary<string,object>>
.
The objects for the dictionary are created using ExpandoObject
and then added to the list with foreach loop. Here is the example of such object:
var dataItem = new ExpandoObject() as IDictionary<string, object>;
dataItem["String Property 1"] = "String Value 1";
dataItem["String Property 2"] = "String Value 2";
dataItem["DateTime Property 1"] = "DateTime Value 1";
dataItem["DateTime Property 2"] = "DateTime Value 2";
From what the method returns, I need to select distinct values of the "DateTime Property 1", but only for date part of it. So, I'm trying to do the following:
var unique = GetData().Select(s => s["DateTime Property 1"].ToShortDateString()).Distinct();
But it says that there is no such method as ToShortDateString()
:
IEnumerable' does not contain a definition for 'ToShortDateString' and no extension method 'ToShortDateString' accepting a first argument of type 'IEnumerable' could be found.
Why is the object
in the dictionary not converted to DateTime
type automatically when property is assigned a DateTime
value? When I use dynamic
everywhere instead of object
, everything works fine.
How do I make it work when using object
?