I have list of object and its relation.
public class Rack
{
public string Name {get;set;}
public List<Product> Products {get;set;}
}
public class Product
{
public string Name {get;set;}
public string Description {get;set;}
}
Now I am trying to make dictionary List of racks but with the key as Product Name and values as Racks ( product belonging to which all racks)
var allRacks = GetAllRacks(); // List of racks
var dict = allRacks
.SelectMany(rack =>
rack.Product.Select(prod => new
{
key = prod.Name,
value = prod.Name,
})
).ToDictionary(x => x.key, x => x.value);
But I get error as key are already added.
So how to select distinct key here or any other way to do that?
Rack Product
R1 P1,P2,P3
R2 P3,P4
R3 P2,P4
And output as dictionary
P1 R1
P2 R1,R3
P3 R1
P4 R2,R3