C# Linq可枚举<;T>;。ElementAt=>;IEnumerable<;T>;

C# Linq可枚举<;T>;。ElementAt=>;IEnumerable<;T>;,c#,linq,C#,Linq,Linq中是否有一个方法与ElementAt相同,只是它返回一个带有单个元素的IEnumerable,而不是实际的元素?难道没有什么SelectRange(startIndex,endIndex)方法可以让我把同一个索引传递两次吗?最简单的方法就是使用 source.Skip(count).Take(1) 或者更一般地说 source.Skip(startIndex).Take(endIndex - startIndex) (假设包含startIndex但不包含endIndex)。最简单的方

Linq中是否有一个方法与
ElementAt
相同,只是它返回一个带有单个元素的
IEnumerable
,而不是实际的元素?难道没有什么
SelectRange(startIndex,endIndex)
方法可以让我把同一个索引传递两次吗?

最简单的方法就是使用

source.Skip(count).Take(1)
或者更一般地说

source.Skip(startIndex).Take(endIndex - startIndex)

(假设包含
startIndex
但不包含
endIndex
)。

最简单的方法是使用

source.Skip(count).Take(1)
或者更一般地说

source.Skip(startIndex).Take(endIndex - startIndex)

(假设包含
startIndex
但不包含
endIndex
)。

Ah。。它被称为
GetRange(index,count)
。我的错。刚找到:)

啊。。它被称为
GetRange(index,count)
。我的错。刚找到:)

写一个扩展方法

    public static IEnumerable<T> ToMyEnumerable<T>(this T input)
    {
        var enumerbale = new[] { input };
        return enumerbale;
    }

    source.First( p => p.ID == value).ToMyEnumerable<T>()
public static IEnumerable to myEnumerable(此T输入)
{
var enumerbale=new[]{input};
返回枚举;
}
source.First(p=>p.ID==value).ToMyEnumerable()

哪个是O(n)

写一个扩展方法

    public static IEnumerable<T> ToMyEnumerable<T>(this T input)
    {
        var enumerbale = new[] { input };
        return enumerbale;
    }

    source.First( p => p.ID == value).ToMyEnumerable<T>()
public static IEnumerable to myEnumerable(此T输入)
{
var enumerbale=new[]{input};
返回枚举;
}
source.First(p=>p.ID==value).ToMyEnumerable()

这就是O(n)

乔恩·斯基特的技术是一个很好的方法。但是,我建议可能的优化是基于
Enumerable.Skip
中的实现细节:它目前似乎没有利用
IList
IList
上的索引器。幸运的是,
Enumerable.ElementAt
可以

因此,另一种解决方案是:

var itemAsSequence = new[] { source.ElementAt(index) };
请注意,这将急切地执行。如果希望延迟执行语义与Jon的答案类似,可以执行以下操作:

public static IEnumerable<T> ElementAtAsSequence<T>
  (this IEnumerable<T> source, int index)
{
   // if you need argument validation, you need another level of indirection

   yield return source.ElementAt(index);
}

...

var itemAsSequence = source.ElementAtAsSequence(index);
公共静态IEnumerable元素序列
(此IEnumerable源,int索引)
{
//如果需要参数验证,则需要另一个间接级别
收益回报来源。元素(指数);
}
...
var itemAsSequence=source.ElementAtAsSequence(索引);

我应该指出,由于这依赖于实现细节,未来LINQ对对象的改进可能会使这种优化变得多余。

Jon Skeet的技术是一种很好的方法。但是,我建议可能的优化是基于
Enumerable.Skip
中的实现细节:它目前似乎没有利用
IList
IList
上的索引器。幸运的是,
Enumerable.ElementAt
可以

因此,另一种解决方案是:

var itemAsSequence = new[] { source.ElementAt(index) };
请注意,这将急切地执行。如果希望延迟执行语义与Jon的答案类似,可以执行以下操作:

public static IEnumerable<T> ElementAtAsSequence<T>
  (this IEnumerable<T> source, int index)
{
   // if you need argument validation, you need another level of indirection

   yield return source.ElementAt(index);
}

...

var itemAsSequence = source.ElementAtAsSequence(index);
公共静态IEnumerable元素序列
(此IEnumerable源,int索引)
{
//如果需要参数验证,则需要另一个间接级别
收益回报来源。元素(指数);
}
...
var itemAsSequence=source.ElementAtAsSequence(索引);

我应该指出,由于这依赖于实现细节,未来LINQ对对象的改进可能会使这种优化变得多余。

不,这是
列表
的一部分,而不是
IEnumerable
,而且它的工作方式会大不相同(正如它所渴望的那样)。@Mark。仅适用于
列表
。Jon的答案适用于任何
IEnumerable
。但在某些情况下+1仍然是一个值得注意的选项。@Mark:它会立即复制项目,而LINQ序列是惰性的-它们只在您开始请求数据时才查看数据。@Mark-这是我的笑话,我想这没那么好笑。:)不,这是
列表
的一部分,而不是
IEnumerable
,而且它的工作方式会有很大的不同(正如它所渴望的那样)。@Mark。仅适用于
列表
。Jon的答案适用于任何
IEnumerable
。但在某些情况下+1仍然是一个值得注意的选项。@Mark:它会立即复制项目,而LINQ序列是惰性的-它们只在您开始请求数据时才查看数据。@Mark-这是我的笑话,我想这没那么好笑。:)如果
source
是一个
列表
,那么
Skip(count)
会是
O(1)
?@Mark:不幸的是,我不相信Skip是为此优化的,没有。真的有点漏洞:(如果
source
是一个
列表
,那么
Skip(count)
会是
O(1)
?@Mark:不幸的是,我不认为Skip是针对这一点优化的,不。真的有点漏洞:(