3

I am having the collection which is dynamic type. I have stored the double values in the collection. For some records, I am not store the data to it. Now I need to get this type as nullable double to perform some operations. Is there any way to get the data property type as nullable while using Expando object ?

ObservableCollection<dynamic> dynamicItems = new ObservableCollection<dynamic>();
for (int j = 1; j <= 4; j++)
{
    dynamic data = new ExpandoObject();
    if (j == 2)
    {
        // not store the value when j is 2.
    }
    else
    {
        data.colValues = 12.2 * j;                  
    }

    dynamicItems.Add(data);
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
Elavarasan M
  • 182
  • 1
  • 11

1 Answers1

3

You can try casting to Double? and then check if colValues == null:

   ...
   if (j == 2)
   {
        // not store the value when j is 2.
       data.colValues = new Nullable<Double>(); // or (Double?) null;
   }
   else
   {
       data.colValues = (Double?) (12.2 * j);                  
   }
   ...

   // if colValues exists
   if (null != data.colValues) {
     Double v = data.colValues;
     ... 
   }

Another way is doing nothing and then check if the field (i.e. colValues) exists, but, IMHO, it's a not that good implementation:

   if (j == 2)
   {
        // not store the value when j is 2. - literally do nothing
   }
   else
   {
       data.colValues = 12.2 * j;                  
   }

   ... 
   // if colValues exists
   if ((data as IDictionary<String, Object>).ContainsKey("colValues")) {
     Double v = data.colValues; // or var v = data.colValues;
     ... 
   }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Hi, can you share how to change this generically ? – Elavarasan M Sep 01 '15 at 00:55
  • @Elavarasan M: the *second* way doesn't depend on `colValues` type at all; the *first* way requires that `colValues` being `struct` – Dmitry Bychenko Sep 01 '15 at 06:34
  • I need to perform some operations based on the the type. But while adding value, i am not giving the type. but when perform some operation, i need to get the type of colvalues as nullable double. is any option to get this ?? Thanks in Advance – Elavarasan M Sep 02 '15 at 06:53
  • @Elavarasan M: `Double?` (or `Nullable`) could be used as *nullable double* (see my 1st chunk of code) – Dmitry Bychenko Sep 02 '15 at 06:56
  • ok. But i am not give its type while store the value. if i get the type of colvalues, its returned double. but actually its nullable double, because i have used expandoobject and not store the value to colvalues in some condition. if i get the type means, i need as nullable double. I mean, in runtime i get the type of colvalues based on the value means its returned the double. can you share, if any suggestion. sorry for the troubles. – Elavarasan M Sep 02 '15 at 07:00
  • @Elavarasan M: if you don't know types (or at least don't want to get them from columns, create nullable *wraps* on values etc.) you can use my *2nd chunk* (it's *type independent*): just do not store values. However, later you have to check if value has been stored or not (and so should be treated as `null`, absent etc.). – Dmitry Bychenko Sep 02 '15 at 07:29
  • In my situation, i get the type based on the colvalues. Now its returned as double. For the same, if i use integer with nullable like (double? colvalues) means the type of colvalues based on the record means it returned as nullable double. Like wise i need to retrieve the type as nullable double with expando object. – Elavarasan M Sep 02 '15 at 10:32
  • @Elavarasan M: eh, if you have `int` you can use `int?` etc. In case you don't want to know anything about the *type* try *the second way* and just put `var v = data.colValues;` instead of `Double v = data.colValues;`. In all the cases you're working with *dynamic* instance, i.e.*late binding* – Dmitry Bychenko Sep 02 '15 at 10:46
  • yes i have used like you mentioned. if i get the type by data.colValues, its returned double only. but i need to retrieve that as nullable, because it have the nullable value. is any way to retreive that as nullable double ? sorry for the lot of confusions. – Elavarasan M Sep 02 '15 at 13:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88667/discussion-between-elavarasan-m-and-dmitry-bychenko). – Elavarasan M Sep 03 '15 at 08:40