YMM YMM - 1 year ago 103
C# Question

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

I have a method that returns


The objects for the dictionary are created using
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

IEnumerable' does not contain a definition for
'ToShortDateString' and no extension method 'ToShortDateString'
accepting a first argument of type 'IEnumerable' could be

Why is the
in the dictionary not converted to
type automatically when property is assigned a
value? When I use
everywhere instead of
, everything works fine.

How do I make it work when using

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download