LINQ的实例

LINQ查询表达式语法:

类型 查询变量 = from 临时变量 in 集合对象或数据库对象
[where 条件表达式]
[order by 条件]
select 临时变量中被查询的值
[group by 条件]

查询变量的作用是保存查询,而非查询结果

查询表达式语法要点总结:

1、查询表达式语法与SQL(结构查询语言)语法相同。

2、查询语法必须以from子句开头,可以以Select或GroupBy子句结束 。

3、使用各种其他操作,如过滤,连接,分组,排序运算符以构造所需的结果。

4、隐式类型变量 - var可以用于保存LINQ查询的结果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //声明一个字符串数组
            string[] reports = { "chinese reports", "english reports", "japanese reports" };

            //创建一个LINQ语句,通过var变量接收结果
            //通过var声明变量,from后面的keys是更换了名字的reports数据源,where用于设置条件语句,select开始查询最终的结果。
            var result = from keys in reports where keys.EndsWith("reports") select keys;

            //输出查询结果
            foreach(var item in result)
            {
                Console.WriteLine(item);
            }
        }
    }
}

posted @ 2020-04-21 14:36  LY-CS  阅读(109)  评论(0编辑  收藏  举报