using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace LinQ
{
class Program
{
static void Main(string[] args)
{
l1();
Console.WriteLine();
l2();
Console.WriteLine();
l3();
Console.WriteLine();
l4();
Console.WriteLine();
l5();
Console.WriteLine();
l6();
Console.WriteLine();
l7();
Console.WriteLine();
l8();
Console.WriteLine();
l9();
Console.WriteLine();
l10();
Console.Read();
}
public static void l1()
{
int[] scores = new int[] { , , , , , , , };
IEnumerable<int> scoreQuery = from scrore in scores where scrore > select scrore;
foreach (int i in scoreQuery)
{
Console.Write(i + " "); }
} /*linq检索数据转换数据
*合并两个序列
*/
class Student
{
public string StuNo { get; set; }
public string StuName { get; set; }
public string StuCollege { get; set; }
public List<int> StuScores;
}
class Teacher
{
public string TeaNo { get; set; }
public string TeaName { get; set; }
public string TeaCollege { get; set; }
}
public static void l2()
{
List<Student> students = new List<Student>() {
new Student{StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo ="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
List<Teacher> teachers = new List<Teacher>() {
new Teacher {TeaNo ="",TeaName ="张华",TeaCollege ="信息工程学院"},
new Teacher {TeaNo ="",TeaName ="王丽",TeaCollege ="机电学院"}
};
var a = (from student in students where student.StuCollege == "信息工程学院" select student.StuName).Concat(from teacher in teachers
where teacher.TeaCollege == "信息工程学院"
select teacher.TeaName);
Console.WriteLine("信息工程学院的老师和同学们有:");
foreach (var person in a)
{
Console.WriteLine(person);
}
}
/*
*选择源序列元素的一个或多个属性构成元素
*/
public static void l3()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores=new List<int> {,,,}},
new Student {StuNo ="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
var a = from student in students select student.StuName;
foreach (var person in a)
{
Console.WriteLine(person);
}
var a1 = from student in students select new { student.StuNo, student.StuName };
foreach (var person in a1)
{
Console.WriteLine("{0},{1}", person.StuNo, person.StuName);
}
} /*
* LINQ的推迟查询
* **/
public static void l4()
{
List<string> Cites = new List<string> { "shanghai", "beijing", "xiamen", "qingdao", "xian" };
var cityWiths = from c in Cites where c.StartsWith("s") orderby c select c;
Console.WriteLine("第一次查询名称以‘S’起始的城市");
foreach (string city in cityWiths)
{
Console.WriteLine(city);
}
Console.WriteLine();
Cites.Add("shengzhen");
Cites.Add("shenyang");
Console.WriteLine("第二次查询名称以‘s’起始的城市");
foreach (string city in cityWiths)
{
Console.WriteLine(city);
}
} /*
* 标准查询操作符
* where 过滤操作符定义了返回元素的条件
* select 投射操作符用于把对象转换成另一个类型的对象
* orderby 排序操作符改变返回元素的顺序
* group by 组合运算符数据放在一个数组中
* Count,Sum,Min,Max,Average 合计操作符计算集合的一个值,利用这些合作操作符,可以计算所有值的总和,元素的个数,值最大和最小的元素,平均值
* Distinct 从集合中删除重复的元素
* join 链接运算符用于链接两个集合
* **/ /*
* where 子句
* **/
public static void l5()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
var a = (from student in students
where student.StuCollege == "信息工程学院" && student.StuScores.Average() >
select student.StuName);
foreach (var person in a)
{
Console.WriteLine(person);
}
}
/*
* orderby子句
* **/
public static void l6()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
var a = from student in students orderby student.StuName descending select student.StuName;
foreach (var person in a)
{
Console.WriteLine(person);
}
}
/*
* group by子句
* **/
public static void l7()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}},
new Student {StuNo ="",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
var a = from student in students
group student by student.StuCollege into collegeGroup
select new { College = collegeGroup.Key, Count = collegeGroup.Count() };
foreach (var person in a)
{
Console.WriteLine("{0}{1}", person.College, person.Count);
}
}
/*
* 合计操作符
**/
public static void l8()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}},
new Student {StuNo ="",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
int a = (from student in students select students).Count();
//Sum,Min,Max,Average原理与其相同
Console.WriteLine("学生总数为:");
Console.WriteLine(a); }
/*
* Distinct运算符
*
**/
public static void l9()
{
List<Student> students = new List<Student>() {
new Student {StuNo ="",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {,,,}},
new Student {StuNo="",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {,,,}},
new Student {StuNo ="",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {,,,}}
};
var a = (from student in students select student.StuCollege).Distinct();
foreach (var college in a)
{
Console.WriteLine(college);
}
} /*
* join用于链接两个集合查询语法为join...in...on....equals
*
**/
class Student3
{
public string StuNo { get; set; }
public string StuName { get; set; }
public string CollegeNo { get; set; }
public List<int> StuScores;
}
class College
{
public string CollegeNo { get; set; }
public string CollegeName { get; set; }
}
public static void l10()
{
List<Student3> students = new List<Student3>() {
new Student3 {StuNo ="",StuName ="张三",CollegeNo ="",StuScores =new List<int> {,,,}},
new Student3 {StuNo="",StuName ="李四",CollegeNo ="",StuScores =new List<int> {,,,}},
new Student3 {StuNo ="",StuName ="王五",CollegeNo ="",StuScores =new List<int> {,,,}}
};
List<College> colleges = new List<College>() {
new College {CollegeNo="",CollegeName ="信息工程学院" },
new College {CollegeNo ="",CollegeName ="机电学院"}
};
var a = from student in students
join college in colleges on student.CollegeNo equals college.CollegeNo
select new { student.StuName, college.CollegeName };
foreach (var person in a)
{
Console.WriteLine("{0 } {1}", person.StuName, person.CollegeName);
} }
}
}

LINQ 操作符的更多相关文章

  1. 委托发展史(Linq操作符)

    嗯~这篇就讲讲Linq吧! 之前讲过Lambda最后进化到了令人发指的地步: Func<string, int> returnLength; returnLength = text =&g ...

  2. linq操作符:分区操作符

    Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作. 一.Take操作符 Take(int n)表示将从序列的开头返回数量为n的连续元素,常用于分页.其 ...

  3. linq操作符:限定操作符

    限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using ...

  4. linq操作符:元素操作符

    元素操作符仅返回一个元素. 一.Fitst操作符 First操作符将返回序列中的第一个元素.如果序列中不包含任何元素,则First<T>方法将引发异常.来看看First()方法的定义: 从 ...

  5. linq操作符:转换操作符

    这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更 ...

  6. linq操作符:聚合操作符

    一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource ...

  7. linq操作符:串联操作符

    串联是一个将两个集合连接在一起的过程.在Linq中,这个过程通过Concat操作符实现.Concat操作符用于连接两个集合,生成一个新的集合.来看看Concat操作符的定义: public stati ...

  8. linq操作符:分组操作符

    分组是根据一个特定的值将序列中的元素进行分组.LINQ只包含一个分组操作符:GroupBy.GroupBy操作符类似于T-SQL语言中的Group By语句.来看看GroupBy的方法定义: publ ...

  9. linq操作符:连接操作符

    linq中的连接操作符主要包括Join()和GroupJoin()两个. 一.Join()操作符 Join()操作符非常类似于T-SQL中的inner join,它将两个数据源进行连接,根据两个数据源 ...

随机推荐

  1. 阻止IOS自动识别页面上的电话号码、email地址

    之前写页面的时候碰到一个很恶心的情况,在6P上数字自动变色,后来找了一些资料: 在iOS的浏览器上,他们有时候会有一些“自作聪明”,自动把页面上的一串数字识别成电话号码,这样用户不小心点击这串数字,就 ...

  2. 关于uisliderview 监听停止滑动的状态

    今天遇到一个问题,做颜色控制的时候,通过slider 改变颜色的亮度.如果直接在slider 上绑定事件,则改变一次就需要向服务器发送一次请求.这种是显然不合理的. 所以使用了下面的解决方法 先将sl ...

  3. What's New in C# 6.0(转)

    原文地址:http://www.codeproject.com/Tips/1023426/Whats-New-in-Csharp 本来想翻译一下贴出来,但是好像很多语言组织起来比较困难,读书少不会表达 ...

  4. C#避免过长的IF和Switch分支的方法

    C#避免过长的IF和Switch分支的方法 1.最蠢形态 //很丑有没有! //这个分支要是一两个还是可以接受的 class Program { static void Main(string[] a ...

  5. ARM指令和Thumb指令区别

    Thumb指令集 ]的问题而提出的,它具有16为的代码密度.Thumb不是一个完整的体系结构,不能指望处理程序只执行Thumb指令而不支持ARM指令集.因此,Thumb指令只需要支持通用功能,必要时, ...

  6. jdk配置环境变量

    介绍在linux下配置jdk环境变量的几种常用方法. 首先在linux下安装jdk,如果出现提示权限不够(且root下也提示权限不够),可用#ls -l filename命令查看一下,如果显示类似如: ...

  7. InetAddress类的使用

    1.1. 简介 IP地址是IP使用的32位(IPv4)或者128位(IPv6)位无符号数字,它是传输层协议TCP,UDP的基础.InetAddress是Java对IP地址的封装,在java.net中有 ...

  8. python 在linux下通过top,和dh命令获得cpu,内存,以及硬盘信息

    主要是通过os.popen读取命令输出实现的,os.popen启动新的进程,且将外部命令的输出作为文件类型对象返回.不能获得外部命令的返回值.既然是文件对象就可以直接用for in 来读取,代码如下: ...

  9. theos初探:ios越狱开发教程

    开发环境搭建回顾 现在已经在windows上安装好了theos了.在上一篇中都已经讲了,开发环境主要部件就是: 1.theos,主要包含了使用make时的makefile模板文件.包含了各种库和框架的 ...

  10. information_schema.engines学习

    当前mysql实例的存储引擎信息可以从information_schema.engines 中查询到 例子: mysql> select * from information_schema.en ...