加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – linq Last()如何工作?

发布时间:2020-12-15 03:52:20 所属栏目:百科 来源:网络整理
导读:我不明白当前可以是null,最后一个可以是一个对象,而最后一个是LINQ函数.我以为最后使用GetEnumerator并持续进行直到当前== null并返回对象.但是,您可以看到第一个GetEnumerator().Current为null,最后以某种方式返回一个对象. linq Last()如何工作? var.GetE
我不明白当前可以是null,最后一个可以是一个对象,而最后一个是LINQ函数.我以为最后使用GetEnumerator并持续进行直到当前== null并返回对象.但是,您可以看到第一个GetEnumerator().Current为null,最后以某种方式返回一个对象.

linq Last()如何工作?

var.GetEnumerator().Current
var.Last()

解决方法

从使用 Reflector在System.Core.dll:
public static TSource Last<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    IList<TSource> list = source as IList<TSource>;
    if (list != null)
    {
        int count = list.Count;
        if (count > 0)
        {
            return list[count - 1];
        }
    }
    else
    {
        using (IEnumerator<TSource> enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                TSource current;
                do
                {
                    current = enumerator.Current;
                }
                while (enumerator.MoveNext());
                return current;
            }
        }
    }
    throw Error.NoElements();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读