-1

I've trying to get the date from LUIS response which comes in a timex key, but I only have managed to get to the list and unable to typecast it back to dictionary. Is there a way to get to the key-value pair.

private const string EntityCustomerID = "CustomerID";
private const string EntityDateOfBirth = "builtin.datetimeV2.date";
private const string EntityNumber = "builtin.number";
private const string DateKeyName = "timex";
private const string ResolutionKeyName = "values";




 public async Task Verification(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
        {
            var message = "";
            EntityRecommendation customerIdEntityRecommendation;
            EntityRecommendation customerDobEntityRecommendation;

            if(result.TryFindEntity(EntityCustomerID, out customerIdEntityRecommendation))
            {
                message =  $"Your customer ID is '{customerIdEntityRecommendation.Entity}'";
            }

            if (result.TryFindEntity(EntityDateOfBirth, out customerDobEntityRecommendation))
            {
                object dateObject;

                if (customerDobEntityRecommendation.Resolution.TryGetValue(ResolutionKeyName, out dateObject))
                {

                    IEnumerable enumerable = dateObject as IEnumerable;
                    if(enumerable != null)
                    {

                        foreach (object element in enumerable)
                        {

                        }
                    }
                    //string dateString = (string)dateObject;
                    //string dateTransformed = DateTime.ParseExact(dateString, "yyyy-MM-dd", null).ToString("MM/dd/yyyy");
                    //message = dateTransformed;

                }
            }
            await context.PostAsync(message);
            context.Wait(MessageReceived);
        }

EDIT 1: I found a way to get the dates via using the following code

foreach (object element in enumerable)
{
 var jObject = (JObject)element;
 var dict = jObject.ToObject<Dictionary<string, object>>();
 var timexValue = dict[DateKeyName];
 }
user6083088
  • 1,047
  • 1
  • 9
  • 27
  • please take care when tagging, this is clearly c# syntax (changed it for you) –  Jul 18 '17 at 14:07
  • @FelixPalmen I could almost swear that I had selected C#, It might have been a honest man mistake. Thank you for rectifying. – user6083088 Jul 18 '17 at 14:09
  • Is this what you are looking => https://github.com/Microsoft/BotBuilder/pull/2964? – Ezequiel Jadib Jul 18 '17 at 14:10
  • @EzequielJadib The problem I'm facing is how to convert the object which is a JArray which has the dictionary inside it. The foreach loop gets me to the dictionary level, but I'm not sure how to read it. – user6083088 Jul 18 '17 at 14:14

1 Answers1

2

The fix for this is being discussed here. In the meantime, you can try to repurpose the code of the fix. Something like:

var children = myArray.Children();

 if (children.Count() == 1)
 {
        return children.First().ToObject<IDictionary<string, object>>();
 }
 else if (children.Count() > 0)
 {
       return children.Select(c => c.ToObject<IDictionary<string, object>>()).ToList();
 }
user6083088
  • 1,047
  • 1
  • 9
  • 27
Ezequiel Jadib
  • 14,767
  • 2
  • 38
  • 43
  • I'll try my best. I'm not sure what is the meaning of repurpose. I'm very new to C# – user6083088 Jul 18 '17 at 14:23
  • 1
    Taking the code of the fix and make it work for you scenario. – Ezequiel Jadib Jul 18 '17 at 14:24
  • How to update the code, isn't that locked in DLL - What am I missing here? – user6083088 Jul 18 '17 at 14:25
  • I thought you had the JArray and wanted to convert that into the dictionary. If you don't have that, then you should wait for the fix to be published or apply the fix and build your own version of BotBuilder (not recommended) – Ezequiel Jadib Jul 18 '17 at 14:28
  • That is precisely what I want to do, I will try to follow your logic in the code snippet you provided. This is all very new to me and I'm a beginner. Thank you for help. Let me try once and I can come back and select your answer. thank you for your time. – user6083088 Jul 18 '17 at 14:31