3

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
halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

4 Answers4

1

Say your class name is Coordinate and your dictionary is Coordinates

Basically:

Coordinates.Add(
  new List<Coordinate>()
  { 
    new Coordinate()
    {
      Id = coordinateId
    }
  }
); //You can remove all the whitespaace

The problem is this way (Since your code checks on if there already exists a list for the key in the dictionary) you will always only have 1 entry in your list.

So why not changes to a Dictionary<string, Coordinates>?

In json terms:

this is what you do:
{  "key" : [Coordinate] }

why not?
{  "key" : Coordinate }

Or are you trying to achieve? 
{  "key" : [Coordinate, Coordinate, Coordinate] }

Answer update

So you do, want the later: { "key" : [Coordinate, Coordinate, Coordinate] }.

You have a List inside each Dictionary entry. If a dictionary is a list of keys and values, Say in 1 dimension a List inside a dictionary is a 2 dimensional Array.

The thing is here. You need to break it up in 2 steps:

  1. Check if the regionId key already exists in the dictionary (This means already 1 coordinate is in there(
  2. If not: make a new list with the first coordinates.
  3. If it does, check if the CoordinateId already exists, if not add it to the list.

Solution

public string MyMethod(string coordinateId, string regionId)
{
    if (!Coordinates.ContainsKey(regionId))
    {
        oordinates.Add(regionId, new Coordinate() {Id = regionId});
    }
    else
    {
        //Get the list of coordinates out of the dictionary for its region
        var list = Coordinates[regionId];
        if(list.Any(cor=> cor.Id == coordinateId))
        {   //Check if any of the coordinates have the same Id
            return "This coordinate id already exist for this region id";

        } else
        {  //Otherwise we add it to the list.
            lists.Add(new Coordinate() { Id = regionId})
        }

    }
    return "added";
}
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
1

Use the correct data structure, a Dictionary<string, HashSet<Coordinates>>. Add the Equals and GetHashCode methods to Coordinates that compare on the id.

That way you can just add another object to the HashSet and it will only get added if it wasn't already there.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171
0

Try something like this: Coordinates.Add(regionId, new List<Coordinates> { new Coordinates { Id = id, Range = ??? } });

0

I think you are looking for this:

Coordinates.Add(regionId, new List<Coordinates>()
                        {
                          new Coordinates(){Id=1,range=10},
                          new Coordinates(){Id=2,range=110},
                          new Coordinates(){Id=3,range=1140}
                       });

If you want to initialize such dictonary means you can do like this:

Dictionary<string, List<Coordinates>> Coordinates = new Dictionary<string, List<Coordinates>>()
{ 
    {"ID1", new List<Coordinates>()
          {
            new Coordinates(){Id=1,range=10},
            new Coordinates(){Id=2,range=110},
            new Coordinates(){Id=3,range=1140}
          }
    },
    {"ID2", new List<Coordinates>()
          {
            new Coordinates(){Id=1,range=10},
            new Coordinates(){Id=2,range=110},
            new Coordinates(){Id=3,range=1140}
          }
    }
};
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88