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.