C# IEnumerable 检索第一条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/955297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:09:52  来源:igfitidea点击:

C# IEnumerable Retrieve The First Record

c#.net

提问by Gavin

I have an IEnumerable list of objects in C#. I can use a for each to loop through and examine each object fine, however in this case all I want to do is examine the first object is there a way to do this without using a foreach loop?

我在 C# 中有一个 IEnumerable 对象列表。我可以使用 for each 循环并检查每个对象,但是在这种情况下,我只想检查第一个对象,有没有办法在不使用 foreach 循环的情况下做到这一点?

I've tried mylist[0] but that didnt work.

我试过 mylist[0] 但这没有用。

Thanks

谢谢

采纳答案by Jon Skeet

(For the sake of convenience, this answer assumes myListimplements IEnumerable<string>; replace stringwith the appropriate type where necessary.)

(为方便起见,此答案假定myListimplements IEnumerable<string>;string在必要时替换为适当的类型。)

If you're using .NET 3.5, use the First()extension method:

如果您使用 .NET 3.5,请使用First()扩展方法:

string first = myList.First();

If you're not sure whether there are any values or not, you can use the FirstOrDefault()method which will return null(or more generally, the default value of the element type) for an empty sequence.

如果您不确定是否有任何值,您可以使用FirstOrDefault()将返回null(或更一般地,元素类型的默认值)的方法用于空序列。

You can still do it "the long way" without a foreachloop:

您仍然可以在没有foreach循环的情况下“长途跋涉” :

using (IEnumerator<string> iterator = myList.GetEnumerator())
{
    if (!iterator.MoveNext())
    {
        throw new WhateverException("Empty list!");
    }
    string first = iterator.Current;
}

It's pretty ugly though :)

不过还是挺丑的 :)

In answer to your comment, no, the returned iterator is notpositioned at the first element initially; it's positioned beforethe first element. You need to call MoveNext()to move it to the first element, and that's how you can tell the difference between an empty sequence and one with a single element in.

回答您的评论,不,返回的迭代器最初定位在第一个元素;它位于第一个元素之前。您需要调用MoveNext()将其移动到第一个元素,这就是您如何区分空序列和具有单个元素的序列之间的区别。

EDIT: Just thinking about it, I wonder whether this is a useful extension method:

编辑:想想看,我想知道这是否是一个有用的扩展方法:

public static bool TryFirst(this IEnumerable<T> source, out T value)
{
    using (IEnumerator<T> iterator = source.GetEnumerator())
    {
        if (!iterator.MoveNext())
        {
            value = default(T);
            return false;
        }
        value = iterator.Current;
        return true;
    }
}

回答by Max Galkin

Remember, there may be no "first element" if the sequence is empty.

请记住,如果序列为空,则可能没有“第一个元素”。

        IEnumerable<int> z = new List<int>();
        int y = z.FirstOrDefault();

回答by Mehrdad Afshari

If you're not on 3.5:

如果您不在 3.5 上:

 using (IEnumerator<Type> ie = ((IEnumerable<Type>)myList).GetEnumerator()) {
     if (ie.MoveNext())
         value = ie.Current;
     else
         // doesn't exist...
 }

or

或者

 Type value = null;
 foreach(Type t in myList) {
     value = t;
     break;
 }