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. SqlBulkCopy类进行大数据(一万条以上)插入测试

    好多天没写博客了,刚刚毕业一个多月! 关于上一篇博客中提到的,在进行批量数据插入数据库的时候可以通过给存储过程传递一个类型为Table的参数进行相关操作,在这个过程中本人没有进行效率的测试.后来查找发 ...

  2. iOS开发~interface Builder(简称 IB) 界面构建器

    1.interface Builder 设置界面 1.1 是什么? 一个可视化的界面编辑工具软件,在xcode4之后整合到了xcode中 1.2 作用? 通过可视化的界面设置,能够少写或不写代码而完成 ...

  3. JS正则表达式---分组

    JS正则表达式---分组 之前写了一篇关于正则新手入门的文章,本以为对正则表达式相对比较了解 但是今天我又遇到了一个坑,可能是自己不够细心的原因吧,今天就着重和大家分享一下javascript正则表达 ...

  4. struts2中的action访问web对象

    Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...

  5. uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!

    I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...

  6. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  7. session问题和JSP

    session问题和JSP 07. 五 / J2EE / 没有评论   一.Session开发中遇到的问题1.内存中的Session非常多,怎么办?2.用户在购物中.服务器停掉了该web应用(或者重新 ...

  8. Js之Navigator对象

    Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象.Navigator对象的命名是为了纪念Netscape之后NavigatorBU览器译注2,不过所有其 ...

  9. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  10. jQuery.data的是jQuery的数据缓存系统

    jQuery.Data源码 jQuery.data的是jQuery的数据缓存系统 jQuery.data的是jQuery的数据缓存系统.它的主要作用就是为普通对象或者DOM元素添加数据. 1 内部存储 ...