C#为同一个键排列多个值

C#为同一个键排列多个值,c#,winforms,collections,dictionary,C#,Winforms,Collections,Dictionary,我正在尝试循环xmlnodecollection,并获取一些google地图标记的值。我正在尝试使用dictionary,但它对集合中的多个节点并不起作用 我希望能够为同一个密钥保存多个键值对。以下是我所拥有的: Dictionary<string, string> mapValues = new Dictionary<string, string>(); foreach (XmlNode node in listProperties)

我正在尝试循环xmlnodecollection,并获取一些google地图标记的值。我正在尝试使用dictionary,但它对集合中的多个节点并不起作用

我希望能够为同一个密钥保存多个键值对。以下是我所拥有的:

        Dictionary<string, string> mapValues = new Dictionary<string, string>();
        foreach (XmlNode node in listProperties)
        {
            row = tblResults.NewRow();                
            row["Id"] = node.Attributes[0].Value;           
            row["Latitude"] = node["Location"].Attributes[0].Value;
            row["Longitude"] = node["Location"].Attributes[1].Value;
            row["City"] = node["Location"].Attributes[2].Value;
            row["Address"] = node["Location"].Attributes[3].Value;
            row["ZipCode"] = node["Location"].Attributes[4].Value;
            row["State"] = node["Location"].Attributes[5].Value;
            mapValues.Add("Latitude", node["Location"].Attributes[0].Value);
            mapValues.Add("Longitude", node["Location"].Attributes[1].Value);
            mapValues.Add("City", node["Location"].Attributes[2].Value);
            mapValues.Add("Address", node["Location"].Attributes[3].Value);
            mapValues.Add("ZipCode", node["Location"].Attributes[4].Value);
            mapValues.Add("State", node["Location"].Attributes[5].Value);

            tblResults.Rows.Add(row);
        }
       GenerateMap(mapValues);
字典映射值=新字典();
foreach(listProperties中的XmlNode节点)
{
row=tblResults.NewRow();
行[“Id”]=节点。属性[0]。值;
行[“纬度”]=节点[“位置”]。属性[0]。值;
行[“经度”]=节点[“位置”]。属性[1]。值;
行[“城市”]=节点[“位置”]。属性[2]。值;
行[“地址”]=节点[“位置”]。属性[3]。值;
行[“ZipCode”]=节点[“位置”]。属性[4]。值;
行[“状态”]=节点[“位置”]。属性[5]。值;
mapValues.Add(“纬度”,节点[“位置])。属性[0]。值);
mapValues.Add(“经度”,节点[“位置])。属性[1]。值);
mapValues.Add(“城市”,节点[“位置])。属性[2]。值);
mapValues.Add(“地址”,节点[“位置])。属性[3]。值);
添加(“ZipCode”,节点[“位置])。属性[4]。值);
mapValues.Add(“状态”,节点[“位置])。属性[5]。值);
tblResults.Rows.Add(row);
}
GenerateMap(映射值);
然后在GenerateMap中,我想使用这些值并将标记放在map对象上:

  private void GenerateMap(Dictionary<string, string> mapInfo)
        {
            gMapControl1.SetCurrentPositionByKeywords("USA");
            gMapControl1.MinZoom = 3;
            gMapControl1.MaxZoom = 17;
            gMapControl1.Zoom = 4;

            gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache;
            gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821);
            gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
            GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1");

            foreach (KeyValuePair<string, string> info in mapInfo)
            {
                PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Value[0]), Convert.ToDouble(info.Value[1]));
                GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl);
                MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver;
                marker.ToolTipMode = mode;
                marker.ToolTipText = info.Value[2] + ", " + info.Value[3] + ", " + info.Value[4] + ", " + info.Value[5];
                address_overlay.Markers.Add(marker);
            }
            gMapControl1.Overlays.Add(address_overlay);
        }
private void GenerateMap(字典mapInfo)
{
GMAPControl 1.SetCurrentPositionByKeywords(“美国”);
gMapControl1.MinZoom=3;
gMapControl1.MaxZoom=17;
gMapControl1.Zoom=4;
gMapControl1.Manager.Mode=GMap.NET.AccessMode.ServerAndCache;
gMapControl1.Position=新的GMap.NET.PointLatLng(29.60862,-82.43821);
gMapControl1.MapProvider=GMap.NET.MapProviders.GoogleMapProvider.Instance;
GMap.NET.WindowsForms.GMapOverlay address_overlay=新的GMap.NET.WindowsForms.GMapOverlay(gMapControl1,“Address1”);
foreach(mapInfo中的KeyValuePair信息)
{
PointLatLng pnl=新的PointLatLng(Convert.ToDouble(信息值[0]),Convert.ToDouble(信息值[1]);
GMapMarkerGoogleRed标记=新的GMapMarkerGoogleRed(pnl);
MarkerTooltipMode=MarkerTooltipMode.OnMouseOver;
marker.ToolTipMode=mode;
marker.ToolTipText=info.Value[2]+”、“+info.Value[3]+”、“+info.Value[4]+”、“+info.Value[5];
地址覆盖。标记。添加(标记);
}
gMapControl1.Overlays.Add(地址\覆盖);
}

我怎样才能做到这一点?我正在Windows窗体应用程序中使用此代码。

您应该创建一个包含所需属性的类,然后创建该类的列表,而不是执行当前正在执行的操作。它简单易读

public class MapValues
{
    public string Latitude { get; set; }
    public string Longitude{ get; set; }
    public string City{ get; set; }
    public string Address{ get; set; }
    public string ZipCode{ get; set; }
    public string State{ get; set; }

    public MapValues(string latitude, string longitude, string city, string address, string zipCode, string state)
    {
        this.Latitude = latitude;
        this.Longitude= longitude;
        this.City= city;
        this.Address= address;
        this.ZipCode= zipCode;
        this.State= state;
    }
}
将代码更改为以下内容:

    List<MapValues> mapValues = new List<MapValues>();
    foreach (XmlNode node in listProperties)
    {
        row = tblResults.NewRow();                
        row["Id"] = node.Attributes[0].Value;           
        row["Latitude"] = node["Location"].Attributes[0].Value;
        row["Longitude"] = node["Location"].Attributes[1].Value;
        row["City"] = node["Location"].Attributes[2].Value;
        row["Address"] = node["Location"].Attributes[3].Value;
        row["ZipCode"] = node["Location"].Attributes[4].Value;
        row["State"] = node["Location"].Attributes[5].Value;

        mapValues.Add(
            new MapValues(
               row["Latitude"],
               row["Longitude"],
               row["City"],
               row["Address"],
               row["ZipCode"],
               row["State"]));

        tblResults.Rows.Add(row);
    }
    GenerateMap(mapValues);
List mapValues=new List();
foreach(listProperties中的XmlNode节点)
{
row=tblResults.NewRow();
行[“Id”]=节点。属性[0]。值;
行[“纬度”]=节点[“位置”]。属性[0]。值;
行[“经度”]=节点[“位置”]。属性[1]。值;
行[“城市”]=节点[“位置”]。属性[2]。值;
行[“地址”]=节点[“位置”]。属性[3]。值;
行[“ZipCode”]=节点[“位置”]。属性[4]。值;
行[“状态”]=节点[“位置”]。属性[5]。值;
mapValues.Add(
新映射值(
第[“纬度”]行,
行[“经度”],
第[“城市”]行,
行[“地址”],
行[“ZipCode”],
行[“状态]);
tblResults.Rows.Add(row);
}
GenerateMap(映射值);
您的更新方法:

    private void GenerateMap(List<MapValues> mapInfo)
    {
        gMapControl1.SetCurrentPositionByKeywords("USA");
        gMapControl1.MinZoom = 3;
        gMapControl1.MaxZoom = 17;
        gMapControl1.Zoom = 4;

        gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache;
        gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821);
        gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
        GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1");

        foreach (MapValues info in mapInfo)
        {
            PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Latitude), Convert.ToDouble(info.Longitude));
            GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl);
            MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver;
            marker.ToolTipMode = mode;
            marker.ToolTipText = info.City + ", " + info.Address + ", " + info.ZipCode + ", " + info.State;
            address_overlay.Markers.Add(marker);
        }
        gMapControl1.Overlays.Add(address_overlay);
    }
private void GenerateMap(列表mapInfo)
{
GMAPControl 1.SetCurrentPositionByKeywords(“美国”);
gMapControl1.MinZoom=3;
gMapControl1.MaxZoom=17;
gMapControl1.Zoom=4;
gMapControl1.Manager.Mode=GMap.NET.AccessMode.ServerAndCache;
gMapControl1.Position=新的GMap.NET.PointLatLng(29.60862,-82.43821);
gMapControl1.MapProvider=GMap.NET.MapProviders.GoogleMapProvider.Instance;
GMap.NET.WindowsForms.GMapOverlay address_overlay=新的GMap.NET.WindowsForms.GMapOverlay(gMapControl1,“Address1”);
foreach(mapInfo中的MapValues信息)
{
PointLatLng pnl=新的PointLatLng(Convert.ToDouble(信息纬度),Convert.ToDouble(信息经度));
GMapMarkerGoogleRed标记=新的GMapMarkerGoogleRed(pnl);
MarkerTooltipMode=MarkerTooltipMode.OnMouseOver;
marker.ToolTipMode=mode;
marker.ToolTipText=info.City+”,“+info.Address+”,“+info.ZipCode+”,“+info.State;
地址覆盖。标记。添加(标记);
}
gMapControl1.Overlays.Add(地址\覆盖);
}

使用
system.linq.xml
ToLookup