C#中Linq查询基本操作使用实例

 

本文介绍Linq查询基本操作(查询关键字)

- from 子句

- where 子句

- select子句

- group 子句

- into 子句

- orderby 子句

- join 子句

- let 子句

- 复合from子句

- 在某些情况下,源序列中的每个元素本身可能是序列(集合),也可能包含序列

- 用语访问单个数据库中的内部集合

- 使用多个from字句执行连接

- 可以包含多个可从独立数据源生成补充查询的from字句

复合(顾名思义就是有多from的字句)实例:

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

namespace 单元测试
{
    class LinqDemo
    {
        static void Main(string[] args)
        {
            List students = new List { 
                new Student{LastName="xiaogui",Scores=new List{97,42,91,60}}, 
                new Student{LastName="xiaozhan",Scores=new List{50,92,81,60}}, 
                new Student {LastName="xiaolan",Scores=new List{32,32,81,90}},
                new Student{LastName="xiaowan",Scores=new List{92,22,81,60}},         
        };
            var query = from stuent in students
                        from score in stuent.Scores orderby score ascending
                        where score > 90
                        select new
                        {
                            Last = stuent.LastName,
                            score
                        };
            foreach (var student in query)//大于90分的显示出来
            {
                Console.WriteLine("{0} Score:{1}", student.Last, student.score);
            }
            Console.ReadLine();
        }
    }

    public class Student
    {
        public string LastName
        {
            get;
            set;
        }
        public List
            Scores
        {
            get;
            set;
        }
    }
    public class Employee
    {
        public string First
        {
            get;
            set;
        }
        public string Last
        {
            get;
            set;
        }
        public int ID
        {
            get;
            set;
        }
    }


}

运行结果: 

C#中Linq查询基本操作使用实例_第1张图片

你可能感兴趣的:(c#开发)