locked
Loop through dictionary RRS feed

  • Question

  • User271039688 posted

    Hello,

    I have the following LINQ query that essentially should group all comments that match a certain product id and store in a dictionary.

    var adjustments = context.adjustments.Where(m => m.adjustment_week_number == WeekNumber && m.adjustment_year == Year && m.dispatch_site_id == DispatchSiteID && m.incinerator_id == IncineratorID).GroupBy(m => m.product_component_id).ToDictionary(x => x.Key, x => x.Select(m => m.adjustment_comments)).Distinct().ToList();

    What I then want to do is to display all comments relating to each product id (there could be several different comments in each case) to screen but cannot figure out how to do this. I have tried the following, which does display the id but for the comments field I get the error: System.Linq.Enumerable+WhereSelectEnumerableIterator.

    MyStringBuilder.Append(string.Join(";", adjustments));

    What am I doing wrong please?

    Friday, July 5, 2019 1:10 PM

All replies

  • User1520731567 posted

    Hi webdeveloper2016,

    According to your descriptions,I suggest you need to keep in mind your type.

    Because there are something wrong during the iteration.

    MyStringBuilder.Append(string.Join(";", adjustments));

    string.Join function can't know what do you wan to iteration in adjustments which is a dictionary type with Key and Value.

    So,I suggest you could add wrap the loop outside the function,for example:

               foreach (var res in adjustments )
                {
                    MyStringBuilder.Append(res.Key + " : " + string.Join("#", res.Value.Select(x => x))+";");
                }

    the output in my project:

    Best Regards.

    Yuki Tao

    Monday, July 8, 2019 7:13 AM
  • User1277055575 posted

    Yes, that's true! I think your solution should be fine.

    Tuesday, July 9, 2019 8:35 AM