从自定义集合项目的检索 [英] Retrieval of items from custom collection
问题描述
我有以下类
public class People
{
public int id;
public string nameHash;
public string name;
}
我需要创建一个自定义集合,包括类的对象人
,这让我找回它的 ID元素
和 nameHash
。收集必须使用的foreach通过其元素进行迭代的能力:
I need to create a custom collection, consisting of objects of class People
, that lets me retrieve elements by its id
and nameHash
. The collection must have the ability to iterate through its elements using foreach:
foreach (People person in PeopleCollection) { ... }
我该怎么办呢?如果你不能给一个详细的答案,至少给行动的简要计划。在此先感谢!
How do I do that? If you can not give a detailed answer, at least give a brief plan of action. Thanks in advance!
推荐答案
如果你在谈论一个大集合,你想基于一个整数更快的查找速度编号
或字符串 NameHash
字段,同时还支持的foreach(富f由于fooCollection)
模式,那么你就可以推出自己的集合,它封装了对字典。原油实现,而不是彻底的测试:
If you're talking about a large collection and you want faster lookups based on an integer Id
or a string NameHash
field while still supporting the foreach (Foo f in fooCollection)
pattern, then you can roll your own collection that wraps a pair of dictionaries. Crude implementation, not thoroughly tested:
class Person
{
public int Id { get; private set; }
public string NameHash { get; private set; }
public string Name { get; private set; }
public Person(int id, string nameHash, string name)
{
Id = id;
NameHash = nameHash;
Name = name;
}
}
class People : IEnumerable<Person>
{
private Dictionary<int, Person> personDictionary = new Dictionary<int, Person>();
private Dictionary<string, int> hashIdMap = new Dictionary<string, int>();
public void Add(Person person)
{
if (person == null)
throw new ArgumentNullException("person");
if (personDictionary.ContainsKey(person.Id))
throw new InvalidOperationException("person Id is already referenced in collection.");
if (hashIdMap.ContainsKey(person.NameHash))
throw new InvalidOperationException("person NameHash is already referenced in collection.");
personDictionary.Add(person.Id, person);
hashIdMap.Add(person.NameHash, person.Id);
}
public Person this[int id]
{
get
{
if (!personDictionary.ContainsKey(id))
throw new ArgumentOutOfRangeException("Id is not in the collection.");
return personDictionary[id];
}
}
public Person this[string nameHash]
{
get
{
if (!hashIdMap.ContainsKey(nameHash))
throw new ArgumentOutOfRangeException("NameHash is not in the collection.");
return this[hashIdMap[nameHash]];
}
}
public IEnumerator<Person> GetEnumerator()
{
foreach (KeyValuePair<int, Person> pair in personDictionary)
yield return pair.Value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
...
...
static void Main()
{
Person personA = new Person(1, "A", "Apple");
Person personB = new Person(2, "B", "Banana");
Person personC = new Person(3, "C", "Cherry");
People people = new People();
people.Add(personA);
people.Add(personB);
people.Add(personC);
Person foo = people[1];
Person bar = people["C"];
Debug.Assert(foo.Name == "Apple");
Debug.Assert(bar.Name == "Cherry");
foreach (Person person in people)
Debug.WriteLine(person.Name);
}
当然,如果你正在处理一个小十岁上下的集合,很可能简单地使用列表< T>
和利用或者LINQ或查找
方法已定义。如
Of course, if you're dealing with a small-ish collection, you could very well simply use a List<T>
and utilize either LINQ or the Find
methods already defined. Such as
Person personA = collection.FirstOrDefault(p => p.Id == 42);
Person personB = collection.Find(p => p.NameHash == "Blah");
这篇关于从自定义集合项目的检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!