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[] { 88, 89, 100, 51, 23, 92, 81, 60 };

IEnumerable<int> scoreQuery = from scrore in scores where scrore > 70
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 ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new
List<int> {97,92,81,60}},
new Student {StuNo ="02",StuName
="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}

};
List<Teacher> teachers = new List<Teacher>()
{
new Teacher {TeaNo ="001",TeaName ="张华",TeaCollege
="信息工程学院"},
new Teacher {TeaNo ="002",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 ="01",StuName
="张三",StuCollege ="信息工程学院",StuScores=new List<int> {97,92,81,60}},

new Student {StuNo ="02",StuName ="李四",StuCollege ="机电学院",StuScores =new
List<int> {90,95,78,70}}
};
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 ="01",StuName ="张三",StuCollege
="信息工程学院",StuScores =new List<int> {97,92,81,60}},
new
Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new
List<int> {90,95,78,70}}
};
var a = (from
student in students
where student.StuCollege == "信息工程学院"
&& student.StuScores.Average() > 75
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 ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new
List<int> {97,92,81,60}},
new Student {StuNo="02",StuName
="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}

};
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 ="01",StuName
="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},

new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new
List<int> {90,95,78,70}},
new Student {StuNo ="03",StuName
="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,90}}

};
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 ="01",StuName ="张三",StuCollege
="信息工程学院",StuScores =new List<int> {97,92,81,60}},
new
Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new
List<int> {90,95,78,70}},
new Student {StuNo ="03",StuName
="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,90}}

};
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 ="01",StuName
="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},

new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new
List<int> {90,95,78,70}},
new Student {StuNo ="03",StuName
="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,70}}

};
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 ="01",StuName ="张三",CollegeNo ="001",StuScores =new
List<int> {97,92,81,60}},
new Student3 {StuNo="02",StuName
="李四",CollegeNo ="002",StuScores =new List<int> {90,95,78,70}},

new Student3 {StuNo ="03",StuName ="王五",CollegeNo ="003",StuScores =new
List<int> {96,95,79,70}}
};

List<College> colleges = new List<College>() {
new
College {CollegeNo="001",CollegeName ="信息工程学院" },
new College {CollegeNo
="002",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);
}

}
}
}

时间: 2024-10-05 19:49:05

LINQ 操作符的相关文章

LINQ操作符一:Select

一.什么是LINQ?它可以用来做什么 语言集成查询(Language Integrated Query,LINQ)是一系列标准查询操作符的集合,这些操作符几乎对每一种数据源的导航.过滤和执行操作都提供了底层的基本查询架构. LINQ可查询的数据源包括XML(可使用LINQ TO XML).关系数据(使用LINQ TO SQL,及先前的DLINQ).ADO.NET DataSet(使用LINQ TO DataSet),以及内存中的数据. 二.投影操作符:Select Select操作符对单个序列或

LINQ操作符二:SelectMany

SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SelectMany操作符 8 { 9 class Program 10 { 1

【LINQ技术】扩展特性和LINQ操作符

LINQ特有的编程结构 LINQ就像是嵌入到C#中的强类型查询语言,尽管和SQL查询很像,但语法却并不相同,甚至还有截然相反的一面. LINQ是在.NET发展到3.5版的时候被引进的,C#和VB语言都为此做了许多工作,扩展了大量新的编程结构. 一.隐式类型本地变量 var--一个如此小巧的关键字却有着强大的力量. var varInt=1; var varBool=True; var varString="String, String, String"; Console.WriteLi

LINQ操作符四:排序操作符

排序操作符,包括OrderBy.OrderByDescending.ThenBy.ThenByDescending和Reverse,提供了升序或者降序排序. OrderBy操作符将序列中的元素按照升序排列. 注意:orderby必须在select之前出现,查询表达式最后只可能出现select或者groupby. student类: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Sy

linq操作符:聚合操作符

一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: 1 public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func); 2 public static TAccumulate Aggregate<TSource, TAccumulate&g

linq操作符:转换操作符

这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以"To"开头的方法可枚举(即时加载)源集合并将项放入相应的集合类型. 一.AsEnumerable操作符 所有实现了IEnumerable<T>接口的类型都可以调用此方法来获取一个IEnumerable<T>集合

linq操作符:限定操作符

限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace LimitOperation

LINQ操作符三:限制操作符

where是限制操作符,它将过滤标准应用在序列上,按照提供的逻辑对序列中的数据进行过滤. where操作符不启动查询的执行.当开始对序列进行遍历时才开始执行,此时过滤条件将被应用到查询中. 示例: //where限制操作符:使用延迟加载 var q = teachers.SelectMany(p => p.Students).Where(s => s.Score < 60).Select(a => new { name = a.Name }); foreach (var item

LINQ 操作符(二)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace Lambda表达式{class Program { static void Main(string[] args) { } static List<Person> GetPersonList() { return new List<Person&g