LINQ之Where

88 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Where()

使用Where()可以获取序列中满足指定条件的所有元素。
MSDN
public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate );

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]       dataA   = new int[] { 0, 1, 2, 3, 4 };
        List<float> dataB   = new List<float>() { 1.5f, 1.3f, 3.2f };
        string[]    dataC   = new string[] { "正一郎", "清次郎", "誠三郎", "征史郎" };

        // 偶数
        IEnumerable<int>    dataA_F = dataA.Where( value => value % 2 == 0 );
        // 小于2
        IEnumerable<float>  dataB_F = dataB.Where( value => value < 2.0f );
        // 长度小于5
        IEnumerable<string> dataC_F = dataC.Where( value => value.Length < 5 );

        System.Console.WriteLine( "dataA       :{0}", dataA.Text() );
        System.Console.WriteLine( "dataA Filter:{0}", dataA_F.Text() );
        System.Console.WriteLine( "dataB       :{0}", dataB.Text() );
        System.Console.WriteLine( "dataB Filter:{0}", dataB_F.Text() );
        System.Console.WriteLine( "dataC       :{0}", dataC.Text() );
        System.Console.WriteLine( "dataC Filter:{0}", dataC_F.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
} 

dataA :[0], [1], [2], [3], [4],
dataA Filter:[0], [2], [4],
dataB :[1.5], [1.3], [3.2],
dataB Filter:[1.5], [1.3],
dataC :[正一郎], [清次郎], [誠三郎], [征史郎],
dataC Filter:[正一郎], [清次郎], [誠三郎], [征史郎],

Where()也可以在条件中处理序列的索引。条件中第二个int参数为索引,具体看下面示例。
MSDN
public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, int, bool> predicate );

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]       dataA   = new int[] { 0, 1, 2, 3, 4 };
        List<float> dataB   = new List<float>() { 1.5f, 1.3f, 3.2f };
        string[]    dataC   = new string[] { "正一郎", "清次郎", "誠三郎", "征史郎" };

        // 索引和value相等
        IEnumerable<int>    dataA_F = dataA.Where( ( value, index ) => value == index );
        // 索引为偶数
        IEnumerable<float>  dataB_F = dataB.Where( ( value, index ) => index % 2 == 0 );
        // 索引为5
        IEnumerable<string> dataC_F = dataC.Where( ( value, index ) => index == 5 );

        System.Console.WriteLine( "dataA       :{0}", dataA.Text() );
        System.Console.WriteLine( "dataA Filter:{0}", dataA_F.Text() );
        System.Console.WriteLine( "dataB       :{0}", dataB.Text() );
        System.Console.WriteLine( "dataB Filter:{0}", dataB_F.Text() );
        System.Console.WriteLine( "dataC       :{0}", dataC.Text() );
        System.Console.WriteLine( "dataC Filter:{0}", dataC_F.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
} 

dataA :[0], [1], [2], [3], [4],
dataA Filter:[0], [1], [2], [3], [4],
dataB :[1.5], [1.3], [3.2],
dataB Filter:[1.5], [3.2],
dataC :[正一郎], [清次郎], [誠三郎], [征史郎],
dataC Filter: