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,它将两个数据源进行连接,根据两个数据源 ...
随机推荐
- jQuery数组处理函数
写在前面: jQuery的数组处理函数整理如下,如有补充和建议,欢迎评论交流~ 1.$.trim(value) 从value中删除任何前导或尾随的空白字符 2.$.each(containe ...
- SqlServer存储过程传入Table参数
今天是周日,刚好有空闲时间整理一下这些天工作业务中遇到的问题. 有时候我们有这样一个需求,就是在后台中传过来一个IList<类>的泛型集合数据,该集合是某个类的实例集合体,然后将该集合中的 ...
- Code Complete阅读笔记(二)
2015-03-06 328 Unusual Data Types ——You can carry this technique to extremes,putting all the ...
- iOS-OC-基础-NSArray常用方法
NSArray常用方法和属性 // ——————————————————————数组常用方法—————————————————————— // 1.计算数组元素的个数: count NSArray * ...
- Telnet运用and Sqlserver connection failed
今天的工作中,需要远程访问服务器上的数据库.但是,连接错误,Error code is 1326.说句实话,关于SqlServer 不能远程访问这个问题,我遇到过N次.可是每次都不认真去研究到底是什么 ...
- javascript版1024游戏源码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- StringList 自定义快速排序
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 创建ListView的基本步骤
参考<疯狂android讲义>第2.5节P94 1.创建一个或者多个ListView <LinearLayout xmlns:android="http://schemas ...
- Python Challenge 过关心得(1)
正式开始第1关,这一关的URL的特殊部分是map. 这关的图片上有一个本子,上面写着K→M,O→Q,E→G,稍微思索就能发现这几个字母都是按照字母表的顺序向后移动了两位,那么最投机取巧的方法就是把ma ...