none
Key not found. RRS feed

  • Question

  • I have a query:

                        var orders = from o in orderHistory
                                     group o by new
                                     {
                                         Category = multipleSpaceRegex.Replace(categoryDictionary[o.Sku].Category.Trim(), " "),
                                         SubCategory = multipleSpaceRegex.Replace(categoryDictionary[o.Sku].SubCategory.Trim(), " ")
                                     } into categoryList
                                     select new
                                     {
                                         Category = categoryList.Key.Category,
                                         SubCategory = categoryList.Key.SubCategory,
                                         Summary = from h in categoryList
                                                   group h by new DateTime(h.Date.Year, h.Date.Month, h.Date.Day) into skuHistory
                                                   orderby skuHistory.Key
                                                   select new { Period = skuHistory.Key, Quantity = skuHistory.Sum(s => s.Quantity) }
                                     };
    

    For reasons that I don't understand yet it turns out that the 'categoryDictionary" might not contain the key o.Sku. In which case a KeyNotFoundException is thrown. Rather than having an exception thrown how would I adjust this query so that some default value is assigned to Category and Subcategory?

    Thank you.

    Kevin
    Tuesday, September 29, 2009 10:37 PM

Answers

  • Hi Kevin,

     

    Glad to see you again!

     

    Can you use the Dictionary.ContainsKey in the LINQ query to check if the certain Dictionary contains the key o.Sku? 

    ==============================================================================
    var orders = from o in orderHistory
                         group o by new
                         {
                             Category = categoryDictionary.ContainsKey(o.Sku) ?
                                                           multipleSpaceRegex.Replace(categoryDictionary[o.Sku].Category.Trim(), “ ”)  :
                                                           SomeDefaultValue,
                             SubCategory = categoryDictionary.ContainsKey(o.Sku) ?
                                                           multipleSpaceRegex.Replace(categoryDictionary[o.Sku].SubCategory.Trim(), “ ”)  :
                                                           SomeDefaultValue
                         } into categoryList
                         ……
    ==============================================================================

     

    Please note: if you are access the DataContext data directly, the query might fail since no appropriate translation of SQL for the Dictionary operations.  Please load the data into local memory by ToList, ToArray or AsEnumerable, etc. 

     

     

    Hope you have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 30, 2009 1:44 AM
    Moderator

All replies

  • Hi Kevin,

     

    Glad to see you again!

     

    Can you use the Dictionary.ContainsKey in the LINQ query to check if the certain Dictionary contains the key o.Sku? 

    ==============================================================================
    var orders = from o in orderHistory
                         group o by new
                         {
                             Category = categoryDictionary.ContainsKey(o.Sku) ?
                                                           multipleSpaceRegex.Replace(categoryDictionary[o.Sku].Category.Trim(), “ ”)  :
                                                           SomeDefaultValue,
                             SubCategory = categoryDictionary.ContainsKey(o.Sku) ?
                                                           multipleSpaceRegex.Replace(categoryDictionary[o.Sku].SubCategory.Trim(), “ ”)  :
                                                           SomeDefaultValue
                         } into categoryList
                         ……
    ==============================================================================

     

    Please note: if you are access the DataContext data directly, the query might fail since no appropriate translation of SQL for the Dictionary operations.  Please load the data into local memory by ToList, ToArray or AsEnumerable, etc. 

     

     

    Hope you have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 30, 2009 1:44 AM
    Moderator
  • Hi Kevin,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, October 2, 2009 11:35 AM
    Moderator