且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于转换IQueryable< T>的扩展方法.和IEnumerable< T>到ReadOnlyCollection< T>

更新时间:2022-02-10 14:50:38

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.