C# linq语句学习

using System;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, 6 };
            var a = from num in arr
                    where num % 2 == 0
                    select num;
            foreach (var i in a)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
            var b = arr.Where(x => x % 2 == 0).OrderBy(n=>n);

            foreach (var i in b)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }
}

使用LINQ查询.NET对象

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> list = new List<Student>()
            {
                new Student() { Name="张三丰", sex="", Age=108 },
                new Student() { Name="李三丰", sex="", Age=108 },
                new Student() { Name="李三丰", sex="", Age=108 }
            };
            var query = from item in list
                        where item.Name.StartsWith("") && item.sex == ""
                        select item;
            foreach(var item in query)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadKey();

        }
    }
}

 查询语句用用查询方法代替: 

var query = list.Where(item => item.Name.StartsWith("") && item.sex == "");

 

查询类型是ArrayList的是需要在变量前增加类型的定义:

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList()
            {
                new Student() { Name="张三丰", sex="", Age=108 },
                new Student() { Name="李三丰", sex="", Age=108 },
                new Student() { Name="李三丰", sex="", Age=108 }
            };
             var query = from Student item in list where item.Name.StartsWith("") && item.sex == "" select item;
            foreach (Student item in query)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadKey();

        }
    }
}

 

用索引器筛选:

             List<Student> list = new List<Student>(){
                     new Student(){ Name="张三丰", Sex="", Age=21 },
                     new Student(){ Name="张三丰", Sex="", Age=21 },
                     new Student(){ Name="李三丰", Sex="", Age=21 },
                     new Student(){ Name="李三丰", Sex="", Age=21 },
                     new Student(){ Name="李四", Sex="", Age=21 },
             };
             var query = list.Where(( item,index )=>item.Name.StartsWith("")&&item.Sex==""&&index%2==0);
             foreach(Student stu in query){
                 Console.WriteLine(stu.Name);
             }

类型筛选:

            object[] arr = {"str",1,3,"mlh"};
            var query = arr.OfType<string>();
            foreach(var item in query){
                Console.WriteLine(item);
            }

 复合的from子句

            List<Student> list = new List<Student>(){
                new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,23} },
                    new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,32 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,35 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,36 } },
                    new Student(){ Name="李四", Sex="", Age=new int[]{ 21,3} },
            };
            var query =from item in list
                from age in item.Age
                where age==3
                select item;
            foreach(Student stu in query){
                Console.WriteLine(stu.Name);
            }

 

select子句:

 

            List<Student> list = new List<Student>(){
                new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,23} },
                    new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,32 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,35 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,36 } },
                    new Student(){ Name="李四", Sex="", Age=new int[]{ 21,3} },
            };
            var query =from item in list
                from age in item.Age
                where age==3
                select item.Name;
            foreach(string item in query){
                Console.WriteLine(item);
            }

 查询方法:

            List<Student> list = new List<Student>(){
                new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,23} },
                    new Student(){ Name="张三丰", Sex="", Age=new int[]{ 21,32 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,35 } },
                    new Student(){ Name="李三丰", Sex="", Age=new int[]{ 21,36 } },
                    new Student(){ Name="李四", Sex="", Age=new int[]{ 21,3} },
            };
            var query = list.Where((item) => {
                    return item.Name.StartsWith("");
                    }).Select(item=>item.Name);
            foreach(string item in query){
                Console.WriteLine(item);
            }

 group by 分组:

            List<Student> list = new List<Student>(){
                new Student(){Name="****",Sex="",Age=new int[]{20,30}},
                    new Student(){Name="***",Sex="",Age=new int[]{20,30}},
                    new Student(){Name="***",Sex="",Age=new int[]{20,30}},
                    new Student(){Name="石盐",Sex="",Age=new int[]{10,11}},
                    new Student(){Name="梅盐",Sex="",Age=new int[]{10,11}},
            };
            var query = from item in list
                group item by item.Sex;foreach(var stu in query){
                foreach(var item in stu){
                    Console.WriteLine(item.Name);
                }
            }

 

posted @ 2019-04-28 22:06  liliyou  阅读(738)  评论(0编辑  收藏  举报