[英]How to get the IQueryable<T> extension methods instead of the IEnumerable<T> extensions?
[英]Extension methods for converting IQueryable<T> and IEnumerable<T> to ReadOnlyCollection<T>
如何编写用于将IQueryable<T>
和IEnumerable<T>
为ReadOnlyCollection<T>
扩展方法?
谢谢
List<T>
已经包含一个扩展方法AsReadOnly()
,因此只需执行以下操作:
queryable.ToList().AsReadOnly()
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堆栈中的所有其他方法一样),您将获得与以前不同的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.