积极答复者
结果集间的转化

问题
答案
-
Hi Starrycheng,
List<T>类自带了一个可以用于转换任何实现了IEnumerable<T>的构造器,请参考:http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx
ObservableCollection<T>类也有一个用于转换List<T>的构造器, 请参考:http://msdn.microsoft.com/en-us/library/ms653202.aspx
因此,可以尝试:
ObservableCollection<String> oc = new ObservableCollection<string>(); List<String> list = new List<string>(oc); ObservableCollection<String> oc2 = new ObservableCollection<string>(list);
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Alexander Sun 2012年6月26日 7:42
-
Hi Starrycheng,
是的, 是可以转化的. 关于资源的消耗, 我们可以分别看下构造器是怎么转化的:
public ObservableCollection(List<T> list) : base((list != null) ? new List<T>(list.Count) : list)
{
this._monitor = new SimpleMonitor<T>();
this.CopyFrom(list);
}
我们来关注下这一句: this.CopyFrom(list);
CopyFrom为如下方法:
private void CopyFrom(IEnumerable<T> collection)
{
IList<T> items = base.Items;
if ((collection != null) && (items != null))
{
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
items.Add(enumerator.Current);
}
}
}
}
从这个方法我们就可以看出, 转换也就是通过对List<T>类型对象的一个个遍历和复制. 所以说转换的消耗取决于要转换目标包含元素的多少.
对于List<T>来说也是相似的, 如下:
public List(IEnumerable<T> collection)
{
if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
ICollection<T> is2 = collection as ICollection<T>;
if (is2 != null)
{
int count = is2.Count;
if (count == 0)
{
this._items = List<T>._emptyArray;
}
else
{
this._items = new T[count];
is2.CopyTo(this._items, 0);
this._size = count;
}
}
else
{
this._size = 0;
this._items = List<T>._emptyArray;
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
this.Add(enumerator.Current);
}
}
}
}
总的来说, 消耗还是取决于IEnumerable<T>包含元素的多少. 由于List<T>和ObservableCollection<T>都继承了IEnumerable<T>, IEnumerable, 使得遍历及互相转换变得比较方便. 希望对你有所帮助.
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Alexander Sun 2012年6月26日 7:42
全部回复
-
Hi Starrycheng,
List<T>类自带了一个可以用于转换任何实现了IEnumerable<T>的构造器,请参考:http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx
ObservableCollection<T>类也有一个用于转换List<T>的构造器, 请参考:http://msdn.microsoft.com/en-us/library/ms653202.aspx
因此,可以尝试:
ObservableCollection<String> oc = new ObservableCollection<string>(); List<String> list = new List<string>(oc); ObservableCollection<String> oc2 = new ObservableCollection<string>(list);
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Alexander Sun 2012年6月26日 7:42
-
Hi Starrycheng,
List<T>类自带了一个可以用于转换任何实现了IEnumerable<T>的构造器,请参考:http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx
ObservableCollection<T>类也有一个用于转换List<T>的构造器, 请参考:http://msdn.microsoft.com/en-us/library/ms653202.aspx
因此,可以尝试:
ObservableCollection<String> oc = new ObservableCollection<string>(); List<String> list = new List<string>(oc); ObservableCollection<String> oc2 = new ObservableCollection<string>(list);
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
ObservableCollection()集合和List()集合是可以相互转化的,对吧??? 它们之间的转化,消耗资源大吗???
Science and technology is my lover.
- 已编辑 starrycheng 2012年6月6日 2:43
-
Hi Starrycheng,
是的, 是可以转化的. 关于资源的消耗, 我们可以分别看下构造器是怎么转化的:
public ObservableCollection(List<T> list) : base((list != null) ? new List<T>(list.Count) : list)
{
this._monitor = new SimpleMonitor<T>();
this.CopyFrom(list);
}
我们来关注下这一句: this.CopyFrom(list);
CopyFrom为如下方法:
private void CopyFrom(IEnumerable<T> collection)
{
IList<T> items = base.Items;
if ((collection != null) && (items != null))
{
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
items.Add(enumerator.Current);
}
}
}
}
从这个方法我们就可以看出, 转换也就是通过对List<T>类型对象的一个个遍历和复制. 所以说转换的消耗取决于要转换目标包含元素的多少.
对于List<T>来说也是相似的, 如下:
public List(IEnumerable<T> collection)
{
if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
ICollection<T> is2 = collection as ICollection<T>;
if (is2 != null)
{
int count = is2.Count;
if (count == 0)
{
this._items = List<T>._emptyArray;
}
else
{
this._items = new T[count];
is2.CopyTo(this._items, 0);
this._size = count;
}
}
else
{
this._size = 0;
this._items = List<T>._emptyArray;
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
this.Add(enumerator.Current);
}
}
}
}
总的来说, 消耗还是取决于IEnumerable<T>包含元素的多少. 由于List<T>和ObservableCollection<T>都继承了IEnumerable<T>, IEnumerable, 使得遍历及互相转换变得比较方便. 希望对你有所帮助.
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Alexander Sun 2012年6月26日 7:42