zoukankan      html  css  js  c++  java
  • LINQ查询表达式---------where子句

    LINQ查询表达式---------where子句

    where 子句用在查询表达式中,用于指定将在查询表达式中返回数据源中的哪些元素。 它将一个布尔条件(“谓词”)应用于每个源元素(由范围变量引用),并返回满足指定条件的元素。 一个查询表达式可以包含多个 where 子句,一个子句可以包含多个谓词子表达式。

    1.1 常见的where子句查询

    static void Main(string[] args)
    {
        int[] numbers = { 9, 7, 5, 8, 3, 4, 6, 2, 1, 0 };
    
        var num1 = from n in numbers
                   where n > 5
                   select n;
    
        foreach (var i in num1)
        {
            Console.Write(i + "  ");
        }
    
        Console.Write("
    ");
        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    
        var num2 = from n in numbers
                   where n > 5
                   where n % 2 == 0
                   select n;
    
        foreach (var i in num2)
        {
            Console.Write(i + "  ");
        }
    
        Console.Write("
    ");
        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    
        var num3 = from n in numbers
                   where n > 5 && n < 9
                   select n;
    
        foreach (var i in num3)
        {
            Console.Write(i + "  ");
        }
    
        Console.Write("
    ");
        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    
        var num4 = from n in numbers
                   where (n == 10 || n < 9) && (n != 7)
                   select n;
    
        foreach (var i in num4)
        {
            Console.Write(i + "  ");
        }
    
        Console.ReadLine();
    }
    输出结果:
    9  7  8  6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    8  6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7  8  6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    5  8  3  4  6  2  1  0
    

     

    1.2 where子句中用自定义函数

    static void Main(string[] args)
    {
        int[] numbers = { 9, 7, 5, 8, 3, 4, 6, 2, 1, 0 };
    
        var num = from n in numbers
                  where IsEva(n)
                  select n;
    
        foreach (var i in num)
        {
            Console.Write(i + "  ");
        }
    
        Console.ReadLine();
    }
    
    public static bool IsEva(int i)
    {
        if (i % 2 == 0)
            return true;
        else
            return false;
    }
    查询结果:
    8  4  6  2  0
    

    1.3 动态微词的筛选

    static void Main(string[] args)
    {
        int[] numbers = { 9, 7, 5, 8, 3, 4, 6, 2, 1, 0 };
    
        //定义动态的谓词数组,这个数组应该由实际运行环境生成
        int[] number = { 7, 5, 3, 1, 0 };
    
        //查询在给定谓词数组里不存在的数字
        var num = from n in numbers
                  where !number.Contains(n)
                  select n;
    
        foreach (var i in num)
        {
            Console.Write(i + "  ");
        }
    
        Console.ReadLine();
    }
    输出结果:
    9  8  4  6  2
    

      

  • 相关阅读:
    leetcode189.轮转数组
    论文阅读笔记A Latent Transformer for Disentangled Face Editing in Images and Videos
    论文阅读笔记StyleCLIP: TextDriven Manipulation of StyleGAN Imagery
    bias variance tradeoff
    .NET类型转换的常用方式
    ASP.NET 页生命周期概述 与 运行page页面时的事件执行顺序 【转】
    查询密码过期时间 oracle
    web2.0开发教程 Ajax Control Toolkit输入提示
    web2.0开发教程1.2(HTML)——HTML4与HTML5的布局
    web2.0技术开发教程1.1(HTML)——HTML基本标签解释
  • 原文地址:https://www.cnblogs.com/haibing0107/p/6485047.html
Copyright © 2011-2022 走看看