YMM YMM - 10 days ago 7
C# Question

Select distinct DateTime properties from List<IDictionary<string,object>> using LINQ

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
?

Answer

Your method returns List<IDictionary<string,object>>, so when you access an item within the Dictionary, the compiler will treat it as an object and so will detect that there is no method ToShortDateString defined on it.

If your method instead returns List<dynamic> and you access an element as a dynamic, the compiler will not check whether or not ToShortDateString exists, and so you will get no errors.

If you know s["DateTime Property 1"] is a DateTime, then you can simply cast it

((DateTime)s["DateTime Property 1"]).ToShortDateString();

Alternatively, you can call ToString then parse the result

DateTime.Parse(s["DateTime Property 1"].ToString()).ToShortDateString();
Comments