不管是在Mvc还是在别的架构中的项目LinQ和Lambda总是经常会遇到的。

而有些LinQ的语法并不是很长用(我大部分用的是Lambda),所以有必要记录一下万一用到的时候我能很方便的找到我想找到的语法。

主要是对LinQ的from、where、select、orderby、group、join,简单的一个介绍

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinQ各种语法
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student(){ StudentID = , Name = "张飞", Age = , Gender = },
new Student(){ StudentID = , Name = "关羽", Age = , Gender = },
new Student(){ StudentID = , Name = "刘备", Age = , Gender = },
new Student(){ StudentID = , Name = "貂蝉", Age = , Gender = },
new Student(){ StudentID = , Name = "孙尚香", Age = , Gender = },
new Student(){ StudentID = , Name = "甄宓", Age = , Gender = },
new Student(){ StudentID = , Name = "大乔", Age = , Gender = },
new Student(){ StudentID = , Name = "小乔", Age = , Gender = },
new Student(){ StudentID = , Name = "马云禄", Age = , Gender = },
new Student(){ StudentID = , Name = "蔡琰", Age = , Gender = },
new Student(){ StudentID = , Name = "祝融", Age = , Gender = },
new Student(){ StudentID = , Name = "关凤", Age = , Gender = },
new Student(){ StudentID = , Name = "隐藏", Age = , Gender = },
new Student(){ StudentID = , Name = "英雄", Age = , Gender = },
};
List<StudentExtension> list1 = new List<StudentExtension>()
{
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "张三"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "李四"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王二"},
new StudentExtension(){ ID = , StudentID = , Range = "汉", TeacherName = "麻子"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "王五"},
new StudentExtension(){ ID = , StudentID = , Range = "魏国", TeacherName = "赵六"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "张三"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "李四"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王二"},
new StudentExtension(){ ID = , StudentID = , Range = "汉", TeacherName = "麻子"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王五"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六11"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六22"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六33"},
}; ///LinQ中的关键字
/// from 指定要查找的数据源及范围变量,多个from子句则表示从多个数据源中查找数据
/// select 自定查询要返回的目标数据,可以指定任何类型,甚至是匿名类型
/// where 指定元素的筛选条件,多个where子句则表示并列条件,必须全部满足才能入选
/// orderby 指定元素的排序字段和排序方式。当有多个排序字段时,有字段顺序确定主次关系,可以指定升序和降序两种排序方式
/// group 指定元素的分组字段
/// join 指定多个数据源的关联方式 ///from子句用来指定数据源
///数据源是实现泛类型接口IEnumerable<T>或IQueryable<T>的类对象。
///from localVar in dataSource
///dataSource 表示数据源
///localVar 表示这个数据源中的其中一个的类型 ///select子句指定目标数据
///指定在执行查询时产生结果的数据集中元素的类型
///select element
///select 是关键字
///element 参数指定查询结果中元素的类型及初始化方式
var linq = from student in list
select new { student.Name, student.Age };
var l = linq.ToList();
foreach (var item in l)
{
Console.WriteLine(item);
}
foreach (var item in l)
{
Console.WriteLine(item.Name + "-" + item.Age);
}
Console.WriteLine("///select子句指定目标数据 返回一个匿名函数 end"); ///where子句指定筛选条件
///对数据源中的数据进行过滤,筛选出满足条件的数据
///where expression
///where 是关键字
///expression 是一个逻辑表达式
var linq2 = from s in list
where s.Age <
select s;
foreach (var item in linq2)
{
Console.WriteLine(item.Name + "-" + item.Age + "-" + item.Gender + "-" + item.StudentID);
}
Console.WriteLine("where子句指定筛选条件 end");
///orderby子句进行排序
///通过orderby子句对查询结果进行排序操作,可以升序或者降序
///orderby element [sortType]
///orderby 是关键字
///element 是需要进行排序的子段
///sortType 是可选字段,可以是ascending升序和descending降序两个选择,默认是升序
var linq3 = from s in list
orderby s.Age
select s;
foreach (var item in linq3)
{
Console.WriteLine(item.Name + "-" + item.Age + "-" + item.Gender + "-" + item.StudentID);
}
Console.WriteLine("orderby子句进行排序 end"); ///group子句进行分组
///group element by key
///group 是关键字
///element 是分组的类型 和 from中德 element 是一个意思
///by 是关键字
///key 分组的字段
var linq1 = from s in list
orderby s.Age descending
group s by s.Age; foreach (var z in linq1)
{
Console.WriteLine(z.Key + "岁");
foreach (var s in z)
{
Console.WriteLine(" " + s.Name);
}
} ///join子句联接
///join可以实现3种类型的联接分别为内部联接、分组联接和左外部联接
///内部联接
/// join element in dataSource on exp1 equals exp2
/// 内联接
/// 使用这种方式,用来匹配的这个字段,两边都必须有一一对应的关键字
/// list和list1这两个列表中,两边必须一一对应上这条数据才会显示在结果集中
var linq4 = from s in list
join c in list1 on s.StudentID equals c.StudentID
select new { s.Name, s.StudentID, s.Age, s.Gender, c.TeacherName, c.Range };
foreach (var item in linq4)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "\t" + item.Range + "\t" + item.TeacherName + "\t");
}
Console.WriteLine("join子句联接---内部联接 end");
///分组联接
/// join element in dataSource on exp1 equals exp2 into grpName
/// 这种方法首先以list为基础联接list1,并且对list1匹配到的数据进行重命名 into grpName
/// (注意这个时候grpName是一个集合,它里面的数据就是和list匹配到的list1的数据,可能匹配到0条或者多条,他的类型就是list1的类型)
/// select new { s.Name, s.StudentID, s.Age, s.Gender, v = grpName};
/// 所以select的时候就要这样写
/// v = 一个 list1 的集合
/// 是用这种方式是一第一种数据源(list)为基础,在那匹配到的第二个数据源(list1)往第一个数据源上拼数据
var linq5 = from s in list
join c in list1 on s.StudentID equals c.StudentID into grpName
select new { s.Name, s.StudentID, s.Age, s.Gender, v = grpName};
foreach (var item in linq5)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "我是Key");
foreach (var item1 in item.v)//item.v中是匹配到的数据集合
{
Console.WriteLine(item1.ID + "\t" + item1.Range + "\t" + item1.StudentID + "\t" + item1.TeacherName);
}
}
Console.WriteLine("join子句联接---分组联接 end");
Console.WriteLine("------------------------------");
///左外部联接
/// join element in dataSource on exp1 equals exp2 into grpName
/// from grp in grpName.DefaultIfEmpty()
/// 注意如果 grp 匹配到的数据为null了,直接报异常
       /// 所以 使用grp里面的数据的时候需要判读grp是否为null,所以这里加一个三元表达式就行了
       ///   grp == null ? "" : grp.Range
            var linq6 = from s in list
join c in list1 on s.StudentID equals c.StudentID into grpName
from grp in grpName.DefaultIfEmpty()
select new { s.Name, s.StudentID, s.Age, s.Gender, grp.TeacherName, grp.Range };
foreach (var item in linq6)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "\t" + item.Range + "\t" + item.TeacherName + "\t");
}
Console.WriteLine("join子句联接---左外部联接 end"); Console.ReadLine();
}
} public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// 1男2女3未设置
/// </summary>
public int Gender { get; set; } }
public class StudentExtension
{
public int ID { get; set; }
public int StudentID { get; set; }
public string Range { get; set; }
public string TeacherName { get; set; }
}
}

Linq 中的模糊查询

            //StartsWith("孙") 相当于孙%
var linq7 = (from s in list
where s.Name.StartsWith("孙")
select s).ToList(); //IndexOf("尚") != -1 相当于%尚%
var linq8 = (from s in list
where s.Name.IndexOf("尚") != -
select s).ToList(); //EndsWith("香") 相当于%香
var linq9 = (from s in list
where s.Name.EndsWith("香")
select s).ToList();

LinQ总结的更多相关文章

  1. Linq表达式、Lambda表达式你更喜欢哪个?

    什么是Linq表达式?什么是Lambda表达式? 如图: 由此可见Linq表达式和Lambda表达式并没有什么可比性. 那与Lambda表达式相关的整条语句称作什么呢?在微软并没有给出官方的命名,在& ...

  2. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  3. [C#] 走进 LINQ 的世界

    走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...

  4. [C#] 进阶 - LINQ 标准查询操作概述

    LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...

  5. LINQ to SQL语句(7)之Exists/In/Any/All/Contains

    适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...

  6. .NET深入实战系列—Linq to Sql进阶

    最近在写代码的过程中用到了Linq查询,在查找资料的过程中发现网上的资料千奇百怪,于是自己整理了一些关于Linq中容易让人困惑的地方. 本文全部代码基于:UserInfo与Class两个表,其中Cla ...

  7. LINQ Group By操作

    在上篇文章 .NET应用程序与数据库交互的若干问题 这篇文章中,讨论了一个计算热门商圈的问题,现在在这里扩展一下,假设我们需要从两张表中统计出热门商圈,这两张表内容如下: 上表是所有政区,商圈中的餐饮 ...

  8. Entity Framework 6 Recipes 2nd Edition(11-9)译 -> 在LINQ中使用规范函数

    11-9. 在LINQ中使用规范函数 问题 想在一个LINQ查询中使用规范函数 解决方案 假设我们已经有一个影片租赁(MovieRental )实体,它保存某个影片什么时候租出及还回来,以及滞纳金等, ...

  9. Entity Framework 6 Recipes 2nd Edition(11-11)译 -> 在LINQ中调用数据库函数

    11-11. 在LINQ中调用数据库函数 问题 相要在一个LINQ 查询中调用数据库函数. 解决方案 假设有一个任命(Appointment )实体模型,如Figure 11-11.所示, 我们想要查 ...

  10. Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询

    问题 你想为多次用到的查询提高性能,而且你不想添加额外的编码或配置. 解决方案 假设你有如Figure 13-8 所示的模型 Figure 13-8. A model with an Associat ...

随机推荐

  1. android native开发时:java.lang.UnsatisfiedLinkError: Native method not found的处理

    这个异常一般是由于JNI的链接器不能正常识别C++的函数名造成的.处理的方法是用exern "C" {},来包裹需要export的C++的native方法. 如果native的方法 ...

  2. COGS731 [网络流24题] 最长递增子序列(最大流)

    给定正整数序列x1,..., xn (n<=500).(1)计算其最长递增子序列的长度s.(2)计算从给定的序列中最多可取出多少个长度为s的递增子序列.(3)如果允许在取出的序列中多次使用x1和 ...

  3. protected(C# 参考)

    protected 关键字是一个成员访问修饰符.受保护成员在它的类中可访问并且可由派生类访问.有关 protected 与其他访问修饰符的比较,请参见可访问性级别. 仅当访问通过派生类类型发生时,基类 ...

  4. 【Linux程序设计】之环境系统函数综合实验

    这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的.贴出来纯粹是聊胜于无. 实验题目:Linux环境下系统函数综合实验 实验目的:熟悉并掌握Linux环境下数学函 ...

  5. BZOJ2674 : Attack

    整体二分+树状数组套Treap,时间复杂度$O(n\log^3n)$. #include<cstdio> #include<cstdlib> #include<algor ...

  6. MySQL安装问题:Unable to update security settings解决方案

    主要问题还是之前装过,卸载的时候卸载不干净导致的. 如下: 安装到最后出现: Unable to update security settings. Access denied for user 'r ...

  7. TYVJ P1001 第K极值 Label:水

    背景 成成第一次模拟赛 第一道 描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数 ...

  8. BZOJ1196: [HNOI2006]公路修建问题

    Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...

  9. 【C语言】16-预处理指令2-条件编译

    条件编译的概念 在很多情况下,我们希望程序的其中一部分代码只有在满足一定条件时才进行编译,否则不参与编译(只有参与编译的代码最终才能被执行),这就是条件编译. 一.基本用法 1 #if 条件1 2 . ...

  10. linux 2.6.37-3.x.x x86_64

    /* * linux 2.6.37-3.x.x x86_64, ~100 LOC * gcc-4.6 -O2 semtex.c && ./a.out * 2010 sd@fuckshe ...