0

in this method I get an exception as there are duplicate key values for productCategory. After referring few articles I understood that to overcome this I have to use Lookup or keyValuePair. What is the best solution to use for my purpose? And I tried using lookup as well as keyValuePair, but I get errors. (some articles I referred, Duplicate keys in .NET dictionaries?, Duplicate keys in .NET dictionaries?, https://learn.microsoft.com/en-us/dotnet/api/system.linq.lookup-2?view=net-5.0)

public async Task<IDictionary<string, ProductSummary>> GetProductDetails(string sId, bool isSplitVersion)
{
    var response = await dbAccess.GetProductDetailsReport(sId);
    Dictionary<string, ProductSummary> ProdListDictionary = new Dictionary<string, ProductSummary>();

    if (isSplitVersion)
    {
        var distinctProductCat = response.GroupBy(c => c.PRODUCT_CATEGORY).ToDictionary(c => c.Key, c => c.ToList());
      
        foreach (var productCategory in distinctProductCat)
        {

            foreach (var item in productCategory)
            {


                ProdListDictionary.Add(productCategory.Key, item);

            }
           

            return ProdListDictionary;


        }

    }
    return productReportMapper.Map(response).ToDictionary(x => x.PRODUCT_CATEGORY, x => x);;
   
}

This is how i tried to convert my existing method.

  public async Task<ILookup<string, ProductSummary>> GetProductDetails(string sId, bool isSplitVersion)
    {

        var response = await dbAccess.GetProductDetailsReport(sId);

        Lookup<string, ProductSummary> ProdListDictionary = new Lookup<string, ProductSummary>();

        if (isSplitVersion)
        {

            var distinctProductCat = response.GroupBy(c => c.PRODUCT_CATEGORY).ToLookup(c => c.Key, c => c.ToList());

            foreach (var productCategory in distinctProductCat)
            {

                foreach (var item in productCategory)
                {


                    ProdListDictionary.Add(productCategory, item);

                }


                return ProdListDictionary;


            }

        }
        return productReportMapper.Map(response).ToLookup(x => x.PRODUCT_CATEGORY, x => x); ;

    }

but i get an errors in the line, ProdListDictionary.Add(productCategory, item);.The error is as " 'Lookup<string, ProductSummary>' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'Lookup<string, ProductSummary>' could be found (are you missing a using directive or an assembly reference?) "

And in Lookup<string, ProductSummary> ProdListDictionary = new Lookup<string, ProductSummary>(); The error is as "Lookup<string, ProductSummary> does not contain a constructor that takes 0 arguments"

Can you please help me to solve the error or if my converted method is wrong, can you please point out my mistakes.

Any help is appreciated. I am new to c#. This is the first time I am using these concepts dictionaries, lookup in c#.

Edited

{
"Value": {
    "category1": [
        {
            ...
            "Product Name": "ABC",
            "Product Category": "category1"
            ...
        }
    ],


   "category2": [
        {
             ...
             "Product  Name": "EFG",
             "Product Category": "category2"
             ...
        },
        {
             ...
             "Product  Name": "XYZ",
             "Product Category": "category2"
             ...
        },
        {
             ...
             "Product  Name": "SDF",
             "Product Category": "category2"
             ...
        }
    ],
 "category3": [
        {
             ...
             "Product  Name": "BNV",
             "Product Category": "category3"
             ...
        },
        {
              ...
             "Product  Name": "DFG",
             "Product Category": "category3"
             ...
        }
    ]

},
"Formatters": [],
"ContentTypes": [],
"DeclaredType": null,
"StatusCode": 200

}

i get the response as above,when a category has only one item,but when it has more than one item for the particular category(As in category2),it throws an exception.

rissa
  • 57
  • 1
  • 7
  • How do you want to handle duplicates? What would you do, ordinarily, if you found 2 items in your product category? Suppose we convert this dupe-intolerant code so it uses a Lookup, and then you look up eg category "A" and you get back two "B"s- what are you going to do? Take the first? Take the second? Average both? – Caius Jard Oct 17 '21 at 17:23
  • @CaiusJard I want both.I have edited in the question,the response that iam expecting. – rissa Oct 17 '21 at 17:42
  • I can't work out why that entire IF block even exists. The last line of the method uses the response delivered on the first line of the method; everything in between appears to be unused – Caius Jard Oct 17 '21 at 18:26
  • Another thing I don't get: you've pasted a block of json, and said "that's the response I get", but the only variable called `response` looks more like it should be an entity from a db query i.e. C# ratehr than JSON. Can you have someone next to you who knows nothing about what you're doing read your question and see if it makes sense to them? It makes nearly no sense to me – Caius Jard Oct 17 '21 at 18:29

0 Answers0