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. overflow清楚浮动 + 去掉li标签的小圆点

    原文链接:http://blog.163.com/qqabc20082006@126/blog/static/22928525201031211212955/ 测试用例: <!DOCTYPE h ...

  2. Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践

    欢迎转载,转载时请保留全文及出处. Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践 Apache安装 下载源程序(http://httpd.ap ...

  3. 真机调试以及“Could not find Developer Disk Image”问题解决方案

    真机测试步骤 1.运行Xcode,Xcode打开后,点左上角菜单'Xcode',点'Preferences'. 2.在打开的窗口中,点'Accounts',切换到账号页,然后点下面的'+'号,在弹出菜 ...

  4. Spring mvc中@RequestMapping 6个基本用法整理

    继续整理,这个是前段时间用jsp开发的一个站点,说起来php程序员去做jsp程序确实有些小不适应,但是弄完后绝对对于这种强类型语言而比收获还是颇多的. 1,最基本的,方法级别上应用 @RequestM ...

  5. HTML5学习笔记一:新增主体结构元素

    Dreamweaver快捷键: 属性面板:Ctrl+F3 新建文档:Ctrl+N 选择用网页查看:F12 新增的主体结构元素: section元素(例子如下): <!DOCTYPE HTML&g ...

  6. php正则验证sql方注入

    <?php function inject_check($Sql_Str) {//自动过滤Sql的注入语句. $check=preg_match('/select|insert|update|d ...

  7. [Head First Python]2. BIF(内置函数)

    1- range() >>> for each in range(2): print(each) ... 0 1

  8. PHP自动生成前端的表单框架

    <?php /** * 为当前所在菜单项样式 * @param string $controller_name * @param string $action_name * @param str ...

  9. 发布(Windows)

    发布(Windows) 本篇将在这个系列演示的例子上继续记录Asp.Net Core在Windows上发布的过程. Asp.Net Core在Windows上可以采用两种运行方式.一种是自托管运行,另 ...

  10. linux多线程编程之互斥锁

    多线程并行运行,共享同一种互斥资源时,需要上互斥锁来运行,主要是用到pthread_mutex_lock函数和pthread_mutex_unlock函数对线程进行上锁和解锁 下面是一个例子: #in ...