Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a question, is it even possible to do this? I have a Dictionary (MapPoint, string) and the string is repeated several times. From that Dictionary, I would like to always extract the one string that is different and find all the MapPoints that belong to it accordingly and put those points in the List. Is there any way to do it?

What I have tried:

For example:
Dictionary input:
C#
{[{ArcGIS.Core.Geometry.MapPoint}, 95835]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95835]}
...
{[{ArcGIS.Core.Geometry.MapPoint}, 95836]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95836]}
..
{[{ArcGIS.Core.Geometry.MapPoint}, 95837]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95837]}
...
{[{ArcGIS.Core.Geometry.MapPoint}, 95838]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95838]}
...
{[{ArcGIS.Core.Geometry.MapPoint}, 95849]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95849]}
...
{[{ArcGIS.Core.Geometry.MapPoint}, 95855]}
{[{ArcGIS.Core.Geometry.MapPoint}, 95855]}
...
I would like to gain from this:
List with values 95835, 95836, 95837, 95838, 95849, 95855.
Then, by individual values, he found which MapPoints belong to them.
And then create, for example, List 95855, where its MapPoints will be.

I will be glad for any help or advice

David
Posted
Updated 28-Sep-22 11:33am
v3
Comments
PIEBALDconsult 27-Sep-22 9:18am    
Make a Dictionary < string , List < ArcGIS.Core.Geometry.MapPoint > > to use as in index.

I'm not exactly sure what you are trying to do as your question isn't clear - there isn't "one string that is different" in your example data.

So I'm going to assume that what you are trying to do is group together the Dictionary entries by the strings "196157" and "196147".
If so, then this may help, with appropriate modifications: Using Linq to create a Dictionary of sub-Lists by grouping from a collection.[^]

If not, then you will need to be a lot clearer about exactly what you are trying to achieve!
 
Share this answer
 
Comments
dejf111 27-Sep-22 3:50am    
I don't know exactly how to explain, English is not my native language but I will try. As stated in the example (in reality, I have over 2000 entries), I would like to take 196157 from the dictionary and accordingly put all the MapPoints that belong to it in the sheet. Same for all the other strings (which I don't know what they are)
Richard Deeming 27-Sep-22 3:58am    
So something like:
foreach (var group in yourDictionary.GroupBy(p => p.Value, p => p.Key, StringComparer.OrdinalIgnoreCase))
{
    string theValue = group.Key;
    foreach (MapPoint point in group)
    {
        ...
    }
}
OriginalGriff 27-Sep-22 4:23am    
If you don't know how to explain it, then give examples of the input and output you want. That's often a lot more readable than an English explanation! :laugh:
dejf111 27-Sep-22 4:42am    
I upgraded the question.
OriginalGriff 27-Sep-22 4:57am    
So, you are grouping them together: follow the link I gave you and it does that: you just need to modify it for your exact requirement.

I would suggest grouping the Dictionary's KeyValuePairs by the Value part of the pair. Then construct a Dictionary where the key is the group key and instead of storing the value as a collection of KeyValuePairs just store it as a collection containing only the key part of the KeyValuePair. In the example, the A group will contain KeyValuePairs (1,A),(4,A). In the new dictionary the key will be A and the value will be the collection (1,4).

C#
private static void Main(string[] args)
  {
      Dictionary<int, string> dict = new() { { 1, "A" }, { 2, "B" },
                         { 3,"C"}, { 4, "A" }, { 5, "B" }, { 6, "C" } };

      //group by the Value item of the KeyValuePair
      var reversedDict = dict.GroupBy(kvp => kvp.Value).
      //the group.Key references the kvp.Value
      //but the group collection (grp) is the original KeyValuePair
      //so need to select only the Key of that KeyValuePair
      ToDictionary(grp => grp.Key, grp => grp.Select(kvp => kvp.Key));

      Console.ReadLine();
  }

You can create a List from a Dictionary by simply calling its ToList method.

 
Share this answer
 
v2
If i understand you well, you want to find key by value(s).

For example, if you want to find keys belongs to 95835 value, use:
C#
Dictionary<System.Drawing.Point, int> d = new Dictionary<System.Drawing.Point, int>();
d.Add(new System.Drawing.Point(1,1), 95835);
d.Add(new System.Drawing.Point(10,10), 95835);
d.Add(new System.Drawing.Point(111,111), 95835);
d.Add(new System.Drawing.Point(20,20), 95836);
d.Add(new System.Drawing.Point(222,222), 95836);
d.Add(new System.Drawing.Point(333,33), 95837);

List<System.Drawing.Point> points = d.Where(x => x.Value==95835)
	.Select(x => x.Key)
	.ToList();


Result (List<Point>):
IsEmpty   X   Y
False     1   1 
False    10  10 
False   111 111


Note: i'm using System.Drawing.Point instead of ArcGIS.Core.Geometry.MapPoint, but the idea is the same.

In case of set of values:
int[] values2find = new int[]{95836, 95837};
List<System.Drawing.Point> foundpoints = d.Where(x => values2find.Any(y => x.Value == y))
	.Select(x => x.Key)
	.ToList();


Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900