有一些标准方法来计算符合某些条件的元素数量:
collection.Where(d=> d==something).Count();
Run Code Online (Sandbox Code Playgroud)
我需要以下功能(伪鳕鱼):
collection.Where(d=> (d==something) && (d.Next == somethingElse)).Count();
Run Code Online (Sandbox Code Playgroud)
编辑:d.Next - 是集合中d之后的下一个元素.
如何实现这一目标?
假设你拥有的是一个涉及源序列中连续元素的谓词,你可以这样做:
int numMatches = collection.Zip(collection.Skip(1), (prev, next) =>
prev == something && next == somethingElse)
.Count(match => match)
Run Code Online (Sandbox Code Playgroud)
在应用过滤器之前,这会将序列叠加在序列的一个延迟版本上.
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |