【问题标题】:Get Sublist from Dictionary Values in c#从 C# 中的字典值中获取子列表
【发布时间】:2018-04-03 00:55:23
【问题描述】:

我有一本字典“roomDict”,想从每个 Room 类中列出字符串属性“roomName”。请帮忙!

这是我之前使用的列表:

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

但现在我已将列表声明为 dict,我似乎无法找出相应的代码:

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

我需要一个字符串列表来填充 Unity 中的下拉列表。提前致谢!

瑞克

【问题讨论】:

    标签: c# list linq dictionary


    【解决方案1】:
    List<string> roomNames = roomDict.Values.Select(rm => rm.roomData.roomName).ToList();
    

    【讨论】:

      【解决方案2】:

      据我了解,您有一个 Dictionary&lt;Guid,Room&gt;,您想将其转换为 Dictionary&lt;Guid,string&gt;,其中的字符串是与 Guid 一起使用的房间的名称。向导当然应该保持不变。

      这可以通过ToDictionary() 轻松完成,方法是更改​​值委托,使其返回Name 属性而不是Room 本身。包含四个元素的简单示例:

      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);
              }
          }
      }
      

      输出:

      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
      

      如果您想尝试一下,这里是link to a Fiddle

      【讨论】:

      • 谢谢,我想我确实要了一份清单,但 dict 似乎更适合手头的任务,然后我会方便地使用 GUID。非常感谢。
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        您可以将对象直接放入组合框中。组合框调用 .ToString 来显示。只需在您的 Room 类中覆盖 .ToString ,您就可以在选择中使用对象的所有属性。

        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}");
                }
        

        使用 Debug.Print 导入 System.Diagnostics

        【讨论】:

        • 我得试试。很不错。非常感谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        • 2013-08-22
        相关资源
        最近更新 更多