简体   繁体   中英

Get Sublist from Dictionary Values in c#

I have a dictionary "roomDict" and would like to make a list of string property "roomName" from each Room class. Please help!

This is what I was using before with a List:

List<string> roomNames = RoomManager.roomDict.Select(rm => rm.roomData.roomName).ToList();

But now I have declared the list as a dict and I can't seem to figure out the corresponding code:

public static Dictionary<Guid, Room> roomDict = new Dictionary<Guid, Room>();

I need a string List to populate a dropdown in Unity. Thanks in advance!

Rik

List<string> roomNames = roomDict.Values.Select(rm => rm.roomData.roomName).ToList();

As I understand it, you have a Dictionary<Guid,Room> that you want to convert to a Dictionary<Guid,string> , where the string is the name of the room that goes with the Guid. The guids of course should remain unchanged.

That can be accomplished easily with ToDictionary() by changing the value delegate so that it returns the Name property instead of the Room itself. Simple example with four elements:

using System;
using System.Linq;
using System.Collections.Generic;

public class Room
{
    public string Name { get; set; }
}

public class Program
{
    public static void Main()
    {
        //Setup sample data
        var roomDictionary = new Dictionary<Guid,Room>
        {
            { Guid.NewGuid(), new Room { Name = "Room A" } },
            { Guid.NewGuid(), new Room { Name = "Room B" } },
            { Guid.NewGuid(), new Room { Name = "Room C" } },
            { Guid.NewGuid(), new Room { Name = "Room D" } }
        };

        //This is the magic line
        var results = roomDictionary.ToDictionary
        ( 
            pair => pair.Key,          //Keep existing key
            pair => pair.Value.Name    //Substitute Name property instead of the Room itself
        );

        //Output results
        foreach (var pair in results)
        {
            Console.WriteLine("{0}={1}", pair.Key, pair.Value);
        }
    }
}

Output:

5cc94e3d-f9d3-448e-ad21-12feee335c2b=Room A
83bc6fca-38b0-4e6c-be3a-2e7be1e11932=Room B
ec9d15dd-0f8b-43b8-9db3-62630cf5821f=Room C
ef08e20c-65e0-43f2-953d-f285380b0a78=Room D

Here is a link to a Fiddle if you want to try it out.

您可以使用Values属性获取房间:

 roomDict.Values.Select(rm => rm.roomData.roomName).ToList();

You can put an object directly into a combo box. The combo box calls .ToString to display. Just override .ToString in your Room Class and you will have all the properties of your object available in the selection.

public class Room
    {
        public  int Width { get; set; }
        public int Length { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
private void btnFillCombo_Click(object sender, EventArgs e)
        {
            foreach (KeyValuePair<int,Room> rm in dctRoom)
            {
                comboBox1.Items.Add(rm.Value);
            }
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Room rm = (Room)comboBox1.SelectedItem;
            Debug.Print($"Name is {rm.Name}, Width is {rm.Width}, Length is {rm.Length}");
        }

to use Debug.Print import System.Diagnostics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM