• LINQ 查询表达式学习


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace TestPage.linq
    {
    public partial class WebForm2 : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    /*-------------------LINQ查询表达式[MSDN]:----------------*\
    | “查询表达式”是用查询语法表示的查询,是一流的语言构造。
    | 它就像任何其他表达式一样,并且可以用在 C# 表达式有效的任何上下文中。
    | 查询表达式由一组用类似于 SQL 或 XQuery 的声明性语法编写的子句组成。
    | 每个子句又包含一个或多个 C# 表达式,而这些表达式本身又可能是查询表达式或包含查询表达式。
    | 查询表达式必须以 from 子句开头,并且必须以 select 或 group 子句结尾。
    | 在第一个 from 子句和最后一个 select 或 group 子句之间,
    | 查询表达式可以包含一个或多个下列可选子句:where、orderby、join、let 甚至附加的 from 子句。
    | 还可以使用 into 关键字使 join 或 group 子句的结果能够充当同一查询表达式中附加查询子句的源。
    \*--------------------------------------------------------------------------
    */
    int[] nums = new int[] { 3, 34, 12, 25, 18, 35, 58, 66, 46, 9 };
    //把数组中大于10并小于50的数升序排列
    IEnumerable<int> query =
    from n
    in nums //数据源nums,范围变量n
    where n > 10 && n < 50 //where子句可以使用 && 和 || 运算符指定任意多个谓词
    orderby n ascending //默认升序ascending,降序descending
    select n;
    foreach (int n in query)
    Response.Write(n
    + " "); //输出:12 18 25 34 35 46


    Response.Write(
    "<br />");//换行--------------------------------
    IEnumerable<int> query2 =
    from n
    in nums
    where IsGt10AndLt50(n) //where 子句可以包含一个或多个返回布尔值的方法
    orderby n ascending
    select n;
    foreach (int n in query2)
    Response.Write(n
    + " "); //输出:12 18 25 34 35 46
    Response.Write("<br />");//换行--------------------------------


    //上面用的是查询语法,可以改为方法语法
    IEnumerable<int> query3 = nums.Where(c => (c > 10 && c < 50)).OrderBy(c => c); //降序用OrderByDescending(c => c)
    foreach (int n in query3)
    Response.Write(n
    + " "); //输出:12 18 25 34 35 46
    Response.Write("<br />");//换行--------------------------------
    var query4 =
    from n
    in nums
    select
    new //使用 select 子句可产生所有其他类型的序列
    {
    desc
    = n > 20 ? "大于20的数:":"小于20的数:",
    num
    = n
    };
    foreach (var n in query4)
    Response.Write(
    string.Format("{0}{1}{2}",n.desc,n.num ,"<br />"));
    /*输出:
    小于20的数:3
    大于20的数:34
    小于20的数:12
    大于20的数:25
    小于20的数:18
    大于20的数:35
    大于20的数:58
    大于20的数:66
    大于20的数:46
    小于20的数:9
    --
    */
    Response.Write(
    "<br />");//换行--------------------------------
    //Reverse()对结果反向排序
    foreach (int n in query.Reverse()) //输出:46 35 34 25 18 12
    Response.Write(n + " ");
    Response.Write(
    "<br />");//换行--------------------------------

    //Skip()跳过指定数量的元素;Take()获取指定数量的元素
    foreach (int n in query.Skip(2).Take(5)) //输出:25 34 35 46
    Response.Write(n + " ");
    int countNum = query.Count(); //总个数:6
    int maxNum = query.Max(); //最大值:46
    int minNum = query.Min(); //最小值:12
    int sumNum = query.Sum(); //总和:170
    int firstNum = query.First();//第一个数:12
    int lastNum = query.Last();//最后一个数:46
    Response.Write("<br />");//换行--------------------------------
    Response.Write("<br />");//换行--------------------------------
    int[] A = { 1, 2, 2, 4, 5, 3 };
    int[] B = { 2, 4, 5, 7, 5, 6, 8 };
    Response.Write(
    "<br />");
    //合并以上两个数组,合并结果不包含重复项,并排序。
    //要求结果为: 1,2,3,4,5,6,7,8
    int[] C = A.Concat(B).Distinct().OrderBy(c => c).ToArray();
    foreach (int i in C)
    Response.Write(i.ToString()
    + " ");
    Response.Write(
    "<br />");//换行--------------------------------
    //去除以上两个数组中所有重复项,并合并,并排序。
    //要求结果为: 1,3,6,7,8
    List<int> D = A.Except(B).Concat(B.Except(A)).OrderBy(c => c).ToList();
    D.ForEach(c
    => Response.Write(c.ToString() + " "));
    }
    bool IsGt10AndLt50(int i)
    {
    return i > 10 && i < 50;
    }
    }
    }
  • 相关阅读:
    不要在股市上浪费时间(够深刻,耽误自己真本事的提高,即使是价值投资也不值得去做)
    在公司里混日子最终伤害的是你自己
    天使投资人的作用
    115太酷了,居然出了个TV版客户端
    QWidget与HWND的互相转换
    Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十五)
    QString的不常见用法
    不要神话创业,什么东西都可以自己做,损失就是不拿工资。如果吃不上饭了,那还是不要创业。服务器很便宜
    C++的try catch到底能防止什么错误?
    迷茫的时候,就随机择一去学,时间不要浪费在选择上了
  • 原文地址:https://www.cnblogs.com/gdjlc/p/2086888.html
Copyright © 2020-2023  润新知