简体   繁体   English

我应该使用什么C#数据结构将不同数量的键/值对映射到字符串列表?

[英]What C# data structure should I use to map a varying number of key/value pairs to a list of strings?

Let's say I want a list of all the addresses in my neighborhood. 假设我想要一个包含邻居所有地址的列表。 And I also want to associate a friend's name and phone number if they live at any of the addresses. 如果他们住在任何一个地址,我也想关联一个朋友的姓名和电话号码。 But not every address has a friend living there, as complete strangers live at many of the addresses, but conversely more than one friend may live at a single address. 但并不是每个地址都有一个朋友住在那里,因为完全陌生人住在许多地址,但相反,不止一个朋友可能住在一个地址。

To summarize, I need both a list of addresses, as well as associations to all of those strings in the form of a name/phone# pair for any friends who live at those addresses. 总而言之,对于住在这些地址的任何朋友,我需要一个地址列表,以及名称/电话号对形式的所有字符串的关联。 The index for searching on this data is only the address, I don't need to access the info by name or by phone number, but I would like to iterate through that data if an address has friends living there. 搜索此数据的索引只是地址,我不需要按名称或电话号码访问信息,但如果地址中有朋友住在那里,我想迭代这些数据。

What is the best C# data structure to use in this situation, and could you perhaps give me some sample code showing it implemented? 在这种情况下使用的最佳C#数据结构是什么,您是否可以给我一些显示它实现的示例代码? Thanks much. 非常感谢。

The Dictionary class is your best choice. Dictionary类是您的最佳选择。 I'd also create a helper class for storing the Name/Phone # key-value pairs, even though you can use one of the native KeyValuePair or Tuple classes. 我还创建了一个用于存储Name / Phone#键值对的辅助类,即使您可以使用其中一个本机KeyValuePair或Tuple类。 In the end, code would look like this: 最后,代码看起来像这样:

class ContactInfo
{
   string Name;
   string TelephoneNumber;
}

Dictionary<string, ICollection<ContactInfo>> addressAndPeopleLivingThere = 
    new Dictionary<string, ICollection<ContactInfo>>();

addressAndPeopleLivingThere.Add("1 Some Street", new List<ContactInfo>());
addressAndPeopleLivingThere["1 Some Street"].Add(new ContactInfo { Name = "Name", TelephoneNumber = "000-000-0000" });

You should create the types representing your domain. 您应该创建代表您的域的类型。 Something like: 就像是:

public class Address{
  public string StreetName{get;set;}
  public List<Person> Friends{get;set;}
}
public class Person{
   public string Name{get;set;}
   public string Phone{get;set;}
}

then you manipulate in lists. 然后你在列表中操纵。

List<Address> addresses = new List<Address>;

    var addWithFriends = from c in addresses where c.Count > 0 select c;

You'll probably want an ILookup<string, Friend> or a Dictionary<string, IEnumerable<Friend>> , which basically amounts to the same thing. 你可能想要一个ILookup<string, Friend>或一个Dictionary<string, IEnumerable<Friend>> ,这基本上是相同的。 Since you want to include all the addresses, construction will be a little more complicated than a simple ToDictionary or ToLookup , but it's not too bad: 由于您想要包含所有地址,因此构造将比简单的ToDictionaryToLookup复杂一点,但它并不太糟糕:

var friendsByAddress = friends.ToLookup(f => f.Address);
var addressesWithFriends = addresses.ToDictionary(
    a => a,
    a => !friendsByAddress.Contains(a) ? Enumerable.Empty<Friend>() : friendsByAddress[a]);

Using this is simple enough: 使用它很简单:

var friendsAtAddress = addressesWithFriends[address];
foreach(var friend in friends) 
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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