请教c#是否有类似于数据库中索引一样的机制 目的主要还是为了增加查询速度,如果有个List<MyClass>,我指定MyClass中的某个属性为索引,然后可以再List中快速检索到该字段,那该多爽,用遍历实在太慢了。。linq中的.Where()查找是不是也是遍历查找? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SortedList<K,T> 等等结构。在内存中通常不搞什么复杂的结构,因为内存的查询遍历很快,你要确定维护额外的一堆结构远比简单地 Array.Sort 或者 List<T>.Sort 快很多,才需要使用额外的一堆结构。 using System.Collections.Generic;public class MyClassCollection : IEnumerable<MyClass>{ private List<MyClass> _Items = new List<MyClass>(); private Dictionary<object, int> _Key = new Dictionary<object, int>(); public int IndexOf(MyClass item) { return _Items.IndexOf(item); } public bool Contains(string key) { return _Key.ContainsKey(key); } public bool Contains(MyClass item) { return _Items.Contains(item); } public MyClass this[int i] { get { if (i >= 0 && i < Count) { return _Items[i]; } else { return null; } } } public MyClass this[object key] { get { if (_Key.ContainsKey(key)) { return _Items[_Key[key]]; } else { return null; } } } public string CommandString { get; set; } public bool Add(MyClass item) { if (_Items.Contains(item)) return false; _Key.Add(item.Key.Trim().ToLower(), _Items.Count); _Items.Add(item); return true; } public bool Remove(object key) { if (_Key.ContainsKey(key) == false) return false; return this.RemoveAt(_Key[key]); } public bool Remove(MyClass item) { if (_Items.Contains(item) == false) return false; return this.RemoveAt(_Items.IndexOf(item)); } public bool RemoveAt(int i) { if (i < 0 || i > _Items.Count - 1) return false; _Key.Remove(_Items[i].Key); _Items.RemoveAt(i); if (i < _Items.Count) { for (int j = i; j <= _Items.Count - 1; j++) { _Key[_Items[j].Key.Trim().ToLower()] = j; } } return true; } public void Clear() { _Items.Clear(); _Key.Clear(); } public int Count { get { return _Items.Count; } } public System.Collections.Generic.IEnumerator<MyClass> GetEnumerator() { return _Items.GetEnumerator(); } public System.Collections.IEnumerator GetEnumerator1() { return GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator1(); }}public class MyClass{ public string Key { get; set; } public string Value1 { get; set; } public string Value2 { get; set; } public string Value3{ get; set; }}发个我自己常用的类,你可以改成多个索引 HashTable、Dictionary,.net3.5还有HashSet这种。 帮忙给点建议 DataTable.select()结果置顶 怎样通过IP得到该IP对应的主机信息 图片预览,如何实现 循环截取全角半角字条串转换为DataTable的问题 放分,太开心了。串口读gps程序,用到多线程,刚做好。开心放分,进者有分~~ c#如何将一个文件夹打包成一个问价,任何文件名,但不是指的压缩文件 请教:C# winform 如何在主窗体的子线程中访问其他子窗体? 请教一个关于BYTE数组的问题 用ArrayList组成的数组为什么显示“未将对象引用设置到对象的实例”的错误? 为什么ExecuteScalar返回object类型? 如何获取调用函数的窗体名?
SortedList<K,T> 等等结构。在内存中通常不搞什么复杂的结构,因为内存的查询遍历很快,你要确定维护额外的一堆结构远比简单地 Array.Sort 或者 List<T>.Sort 快很多,才需要使用额外的一堆结构。
using System.Collections.Generic;
public class MyClassCollection : IEnumerable<MyClass>
{
private List<MyClass> _Items = new List<MyClass>();
private Dictionary<object, int> _Key = new Dictionary<object, int>();
public int IndexOf(MyClass item)
{
return _Items.IndexOf(item);
}
public bool Contains(string key)
{
return _Key.ContainsKey(key);
}
public bool Contains(MyClass item)
{
return _Items.Contains(item);
}
public MyClass this[int i]
{
get
{
if (i >= 0 && i < Count)
{
return _Items[i];
}
else
{
return null;
}
}
}
public MyClass this[object key]
{
get
{
if (_Key.ContainsKey(key))
{
return _Items[_Key[key]];
}
else
{
return null;
}
}
}
public string CommandString { get; set; }
public bool Add(MyClass item)
{
if (_Items.Contains(item))
return false;
_Key.Add(item.Key.Trim().ToLower(), _Items.Count);
_Items.Add(item);
return true;
}
public bool Remove(object key)
{
if (_Key.ContainsKey(key) == false)
return false;
return this.RemoveAt(_Key[key]);
}
public bool Remove(MyClass item)
{
if (_Items.Contains(item) == false)
return false;
return this.RemoveAt(_Items.IndexOf(item));
}
public bool RemoveAt(int i)
{
if (i < 0 || i > _Items.Count - 1)
return false;
_Key.Remove(_Items[i].Key);
_Items.RemoveAt(i);
if (i < _Items.Count)
{
for (int j = i; j <= _Items.Count - 1; j++)
{
_Key[_Items[j].Key.Trim().ToLower()] = j;
}
}
return true;
}
public void Clear()
{
_Items.Clear();
_Key.Clear();
}
public int Count
{
get { return _Items.Count; }
} public System.Collections.Generic.IEnumerator<MyClass> GetEnumerator()
{
return _Items.GetEnumerator();
} public System.Collections.IEnumerator GetEnumerator1()
{
return GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator1();
}
}
public class MyClass
{
public string Key { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3{ get; set; }
}发个我自己常用的类,你可以改成多个索引
.net3.5还有HashSet这种。