![](/img/trans.png)
[英]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.