C#里怎么样使用where方法2?
C#里怎么样使用where方法2?
在开发里,如果在无序的队列里,要找到某类元素,
往往需要循环地遍历所有元素,然后判断这个元素是否符合所需要的条件。
在C#里提供了LINQ和where方法,就可以有另外两种选择了。
不过,如果是在有序的队列里,去找某个元素,最好是使用二分查找法,它们比前面三种方式都会快得多。
Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引。
使用索引的方法,也可以写得比较复杂,如下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> intList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };</