c# SortedList的使用
2008-11-18 10:19
239 查看
对IDictionary进行操作是,发现用SortedList也可以等同于 ArryList和Hashtable的结合,只是通过Key排序,key不允许为空和null
value可以,在效率上没有测试,但是保证是低,必定在插入时要比较排序。
通过公用方法得到信息(得到1行数据):
public IDictionary ExecuteDictionary( IDbCommand iCmd )
{
IDataReader reader = null;
try
{
//只读取一行数据,第一行
reader = iCmd.ExecuteReader( CommandBehavior.SingleRow );
}
catch(Exception e)
{
this.Close( iCmd );
return null;
}
IDictionary dict = null;
if(reader.Read())
{
int fieldCount = reader.FieldCount;
dict = new SortedList( fieldCount );
for(int i = 0; i < fieldCount; i++)
{
dict [reader.GetName( i ).ToLower()] = reader.GetValue( i );
}
}
reader.Close();
reader.Dispose();
return dict;
}
//返回list
public SortedList selectSingln()
{
DB.CommandText = @" SELECT TOP 5 * FROM products";
DB.CommandType = CommandType.Text;
return (SortedList)DB.ExecuteDictionary();
}
//遍历list
private void _BeginRun()
{
_SqlServerLogic logic = new _SqlServerLogic();
SortedList dic = logic.selectSingln();
Hashtable hash = new Hashtable();
//遍历sortlist
foreach(DictionaryEntry entry in dic)
{
Response.Write( entry.Key + "***" + entry.Value + "<br>" );
if( !string.IsNullOrEmpty( entry.Value.ToString() ) )
{
hash.Add( entry.Key, entry.Value );
}
}
IDictionaryEnumerator item = hash.GetEnumerator();
//遍历Hashtable
while( item.MoveNext() )
{
Response.Write( item.Key +"-----"+ item.Value +"<br/>" );
}
string [] ary = new string [dic.Count];
dic.Keys.CopyTo( ary, 0 );
Response.Write( string.Join( ",", ary ) );
//for 遍历list
for(int i = 0; i < dic.Count; i++)
{
Response.Write( dic.GetKey(i)+"----"+ dic.GetByIndex(i) +"<br/>" );
}
}
value可以,在效率上没有测试,但是保证是低,必定在插入时要比较排序。
通过公用方法得到信息(得到1行数据):
public IDictionary ExecuteDictionary( IDbCommand iCmd )
{
IDataReader reader = null;
try
{
//只读取一行数据,第一行
reader = iCmd.ExecuteReader( CommandBehavior.SingleRow );
}
catch(Exception e)
{
this.Close( iCmd );
return null;
}
IDictionary dict = null;
if(reader.Read())
{
int fieldCount = reader.FieldCount;
dict = new SortedList( fieldCount );
for(int i = 0; i < fieldCount; i++)
{
dict [reader.GetName( i ).ToLower()] = reader.GetValue( i );
}
}
reader.Close();
reader.Dispose();
return dict;
}
//返回list
public SortedList selectSingln()
{
DB.CommandText = @" SELECT TOP 5 * FROM products";
DB.CommandType = CommandType.Text;
return (SortedList)DB.ExecuteDictionary();
}
//遍历list
private void _BeginRun()
{
_SqlServerLogic logic = new _SqlServerLogic();
SortedList dic = logic.selectSingln();
Hashtable hash = new Hashtable();
//遍历sortlist
foreach(DictionaryEntry entry in dic)
{
Response.Write( entry.Key + "***" + entry.Value + "<br>" );
if( !string.IsNullOrEmpty( entry.Value.ToString() ) )
{
hash.Add( entry.Key, entry.Value );
}
}
IDictionaryEnumerator item = hash.GetEnumerator();
//遍历Hashtable
while( item.MoveNext() )
{
Response.Write( item.Key +"-----"+ item.Value +"<br/>" );
}
string [] ary = new string [dic.Count];
dic.Keys.CopyTo( ary, 0 );
Response.Write( string.Join( ",", ary ) );
//for 遍历list
for(int i = 0; i < dic.Count; i++)
{
Response.Write( dic.GetKey(i)+"----"+ dic.GetByIndex(i) +"<br/>" );
}
}
相关文章推荐
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queue<T>,Stack<T>等
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,
- C# 中使用有序表SortedList
- C#使用集合组织数据(HashTable、ArrayList、List<T>,Dictionary<K,V>
- C#使用List.Clear()方法可以让GC回收内存吗?
- C#使用List类实现动态变长数组的方法
- C#中collection,list,map的使用
- C# 中使用LINQ和LAMBDA表达式对LIST进行多字段排序
- 深入理解 c# 第一章 使用IComparer<Product>委托 对 List <Product> 进行排序
- C#集合类详解,Hashtable、Dictionary、SortedList、SortedLi、Array、ArrayList、List、Stack、SortedDictionar
- C# list使用方法
- C# list使用方法
- C# 使用List泛型读取和保存文本文件
- C# list使用方法
- c# SortedList的妙用 (GroupBy)
- C#中使用List类
- C# 中使用Linq和Lambda表达式对List<T>进行排序
- C#使用ListJson通过php和平台进行通信
- C# 使用List泛型读取和保存文本文件
- c# 中的arrlist queue HashtableTest SortedList stack 集合