用于转换IQueryable< T>的扩展方法.和IEnumerable< T>到ReadOnlyCollection< T> [英] Extension methods for converting IQueryable<T> and IEnumerable<T> to ReadOnlyCollection<T>
本文介绍了用于转换IQueryable< T>的扩展方法.和IEnumerable< T>到ReadOnlyCollection< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写将IQueryable<T>
和IEnumerable<T>
转换为ReadOnlyCollection<T>
的扩展方法?
How to write extension methods for converting IQueryable<T>
and IEnumerable<T>
to ReadOnlyCollection<T>
?
谢谢
推荐答案
public static ReadOnlyCollection<T> AsReadOnlyCollection<T>(this IEnumerable<T> source)
{
if(source == null)
throw new ArgumentNulLException("source");
IList<T> list = source as IList<T> ?? source.ToList();
return new ReadOnlyCollection<T>(list);
}
请注意,在这种情况下,没有转换" IEnumerable<T>
之类的事情(与LINQ堆栈中的所有其他方法一样),您将获得与以前不同的对象.
Note that there is no such thing as "converting" an IEnumerable<T>
in this case (as with all other methods in the LINQ stack), you will get back a different object than before.
这篇关于用于转换IQueryable< T>的扩展方法.和IEnumerable< T>到ReadOnlyCollection< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文