I am having a below dictionary :
Dictionary<string, List<Coordinates>> Coordinates = new Dictionary<string,List<Coordinates>>();
public class Coordinates
{
public int Id { get; set; }
public decimal range { get; set; }
//other properties
}
Now I am trying to add coordinates to my dictionary for specific region id and if same coordinates id comes then I don't want to add it.
Below is my web service method:
public string MyMethod(string coordinateId, string regionId)
{
var coordinates = Coordinates.Where(ed => (ed.Key == regionId)).SelectMany(ed => ed.Value).ToList();
var coordinatesExist = coordinates.Any(ed => ed.Id.ToString() == id);
if (!Coordinates.ContainsKey(regionId) && !coordinatesExist)
{
//here i want to add it but as it is list of my class i dont know how to add it
Coordinates.Add(regionId, ?????);
}
else
{
return "This coordinate id already exist for this region id";
}
}
I have check below link but those answers are with the list of string or with an array but not with the list of class:
c# dictionary How to add multiple values for single key?
How to add values to Dictionary with a list of dictionaries inside
Sample Data :
Region Id = 100
Coordinate Id = 10,20,30
Region Id = 200
Coordinate Id = 10,20,30
- 1st call to Mymethod with RegionId = 100 and coordinate id =10 then I would like to have in my dictionary:Key = 100 , value = 10
- 2nd call to Mymethod with RegionId = 100 and coordinate id =20 then I would like to have in my dictionary:Key = 100 , value = 20
- 3rd call to Mymethod with RegionId = 100 and coordinate id =20 then I would like to have in my dictionary: I don't want to add this as 20 coordinate id already exist for regionId = 100