LINQ 操作符
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 操作符的更多相关文章
- 委托发展史(Linq操作符)
嗯~这篇就讲讲Linq吧! 之前讲过Lambda最后进化到了令人发指的地步: Func<string, int> returnLength; returnLength = text =&g ...
- linq操作符:分区操作符
Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作. 一.Take操作符 Take(int n)表示将从序列的开头返回数量为n的连续元素,常用于分页.其 ...
- linq操作符:限定操作符
限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using ...
- linq操作符:元素操作符
元素操作符仅返回一个元素. 一.Fitst操作符 First操作符将返回序列中的第一个元素.如果序列中不包含任何元素,则First<T>方法将引发异常.来看看First()方法的定义: 从 ...
- linq操作符:转换操作符
这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更 ...
- linq操作符:聚合操作符
一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource ...
- linq操作符:串联操作符
串联是一个将两个集合连接在一起的过程.在Linq中,这个过程通过Concat操作符实现.Concat操作符用于连接两个集合,生成一个新的集合.来看看Concat操作符的定义: public stati ...
- linq操作符:分组操作符
分组是根据一个特定的值将序列中的元素进行分组.LINQ只包含一个分组操作符:GroupBy.GroupBy操作符类似于T-SQL语言中的Group By语句.来看看GroupBy的方法定义: publ ...
- linq操作符:连接操作符
linq中的连接操作符主要包括Join()和GroupJoin()两个. 一.Join()操作符 Join()操作符非常类似于T-SQL中的inner join,它将两个数据源进行连接,根据两个数据源 ...
随机推荐
- js简单排序
简单的排序功能 HTML代码: <body> <input id="btn1" type="button" value="排序&qu ...
- .responsiveSlides参数
$(".rslides").responsiveSlides({ auto: true, // Boolean: Animate automatically, true or fa ...
- 自己对WSO2 ESB 见解
这周没想到要更新什么内容,就把我最近工作接触的WSO2 ESB简单介绍下吧. 前提: 一切文档,知识都要与官方文档为准. WSO2 ESB: http://wso2.com/products/ ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- Java的反射机制及应用实例
一:什么是反射机制 简单的来说,反射机制指的是程序在运行时能够获取自身的信息.在Java中,只要给定类的名字,那么就可以通过反射机制来获得类的所有信息. 二:哪里用到反射机制 我们用过一些知识,但是并 ...
- JDK,TomCat安装配置
JDK.Tomcat.myEclipse安装配置 准备安装包 JAVA运行环境包 JDK1.7下载地址: http://www.veryhuo.com/down/html/43205.html Jsp ...
- 【solr基础教程之一】Solr相关知识点串讲
Solr是Apache Lucene的一个子项目.Lucene为全文搜索功能提供了完备的API,但它只作为一个API库存在,而不能直接用于搜索.因此,Solr基于Lucene构建了一个完 ...
- PHP PSR-3 日志接口规范 (中文版)
日志接口规范 本文制定了日志类库的通用接口规范. 本规范的主要目的,是为了让日志类库以简单通用的方式,通过接收一个 Psr\Log\LoggerInterface 对象,来记录日志信息. 框架以及CM ...
- Web程序工作原理
1.Web程序工作原理 (1)Web一词的含义 Network:[计算机]电脑网络,网 Web:[计算机]万维网(World Wide Web),互联网(Internet) Web程序,顾名思义,即工 ...
- Obstack是C标准库里面对内存管理的GNU扩展
Obstack是C标准库里面对内存管理的GNU扩展 Obstack介绍 Obstack初始化 在Obstack中申请对象 释放对象 申请growing object 获取Obstack状态 数据对齐 ...