using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace First_exam
{
class Student
{
/// <summary>
///学生姓名的字段和属性
/// </summary>
private string _Name;
public string Name
{
get { return this._Name; }
}
private string _Xingbie;
/// <summary>
/// 学生性别的字段和属性
/// </summary>
public string Xingbie
{
get { return this._Xingbie; }
}
/// <summary>
/// 学生年龄的字段和属性
/// </summary>
private uint _Age;
public uint Age
{
get { return this._Age; }
}
/// <summary>
/// 学生成绩的字段和属性
/// </summary>
private List<LessonScore> _Scores;
public List<LessonScore> Scores
{
get { return this._Scores; }
}
/// <summary>
/// 构造函数,传入学生姓名,性别,年龄,成绩
/// </summary>
public Student(string name, string xb, uint age, List<LessonScore> scrs)
{
this._Name = name;
this._Xingbie = xb;
this._Age = age;
this._Scores = scrs;
}
public Student(string name, string xb, uint age)
{
this._Name = name;
this._Xingbie = xb;
this._Age = age;
this._Scores = null;
}
public override string ToString()
{
string str;
str = string.Format("{0}--{1}--{2}",this._Name, this._Age,this._Xingbie);
return str;
} } class LessonScore
{
/// <summary>
/// 课程成绩的字段和属性
/// </summary>
private float _Score;
public float Score
{
get { return this._Score; }
}
/// <summary>
/// 课程名称的字段和属性
/// </summary>
private string _Lessson;
public string Lesson
{
get { return this._Lessson; }
}
/// <summary>
/// 构造函数
/// </summary>
public LessonScore(string les, float scr)
{
this._Lessson = les;
this._Score = scr;
} public override string ToString()
{
string str;
str = string.Format("{0}----{1}分", this._Lessson,this._Score);
return str;
}
}
class Program
{
static void Main(string[] args)
{
Student[] arr =
{
new Student("张氏那","男",),
new Student("李四","男",),
new Student("李霞","女",),
new Student("王妈妈","女",),
new Student("郭明","男",),
new Student("欧阳夏","女",),
new Student("王丹","女",),
new Student("张宝","男",),
}; var query =
from val in arr
select val;
foreach (Student item in query)
{
Console.WriteLine(item);
}
Console.WriteLine(); var query2 =
from val in arr
select val.Name;
foreach(string item in query2)
{
Console.Write("{0}, ",item);
}
Console.WriteLine();
Console.WriteLine(); var query3 =
from val in arr
select val.Name.Length;
foreach (int item in query3)
{
Console.Write("{0}, ", item);
}
Console.WriteLine();
Console.WriteLine(); var query4 =
from val in arr
select new { val.Name, val.Age, NameLen = val.Name.Length};
foreach(var item2 in query4)
{
Console.WriteLine(item2);
}
Console.WriteLine(); int[] ary = { ,,,,,,,,,,,,,,};
var query5 =
from val in ary
orderby val
select val;
foreach(var item in query5)
{
Console.Write("{0} ",item);
}
Console.WriteLine();
Console.WriteLine();
var query6 =
from val in ary
orderby val descending
select val;
foreach (var item in query6)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
Console.WriteLine(); var query7 =
from val in arr
orderby val.Name.Length ascending, val.Age descending
select val;
foreach(var item in query7)
{
Console.WriteLine(item);
}
Console.WriteLine(); var query8 =
from val in arr
group val by val.Xingbie;
foreach(var grp in query8)
{
Console.WriteLine(grp.Key);
foreach(var st in grp)
{
Console.WriteLine("\t{0}",st);
}
}
Console.WriteLine(); var query9 =
from val in arr
group val by val.Age into stgrp
orderby stgrp.Key descending
select stgrp;
foreach(var st in query9)
{
Console.WriteLine("{0}岁的学生:",st.Key);
foreach(var stu in st)
{
Console.WriteLine("\t{0}",stu);
}
}
Console.WriteLine();
Console.WriteLine("**************************************************************************");
Console.WriteLine(); /*
*from子句的复合查询
*/
Student[] stAry = {
new Student("张氏那","男",,
new List<LessonScore>{
new LessonScore("语文",80.5f),
new LessonScore("数学",90f),
new LessonScore("英语",89f)
}),
new Student("李四","男",,
new List<LessonScore>{
new LessonScore("语文",86.5f),
new LessonScore("数学",97f),
new LessonScore("英语",95f)
}),
new Student("李霞","女",,
new List<LessonScore>{
new LessonScore("语文",76f),
new LessonScore("数学",85f),
new LessonScore("英语",79f)
}),
new Student("王妈妈","女",,
new List<LessonScore>{
new LessonScore("语文",85f),
new LessonScore("数学",87f),
new LessonScore("英语",79f)
}),
new Student("郭明","男",,
new List<LessonScore>{
new LessonScore("语文",69f),
new LessonScore("数学",72f),
new LessonScore("英语",83f)
}),
new Student("欧阳夏","女",,
new List<LessonScore>{
new LessonScore("语文",85f),
new LessonScore("数学",85f),
new LessonScore("英语",79f)
}),
new Student("王丹","女",,
new List<LessonScore>{
new LessonScore("语文",73f),
new LessonScore("数学",77f),
new LessonScore("英语",80f)
}),
new Student("张宝","男",,
new List<LessonScore>{
new LessonScore("语文",88f),
new LessonScore("数学",89f),
new LessonScore("英语",94f)
}),
}; var query10 =
from st in stAry
from scr in st.Scores
where scr.Score >
group new { st.Name, scr } by st.Name;
foreach(var grp in query10)
{
Console.WriteLine(grp.Key);
foreach(var item in grp)
{
Console.WriteLine("\t{0}",item);
}
} Console.WriteLine();
Console.WriteLine("**************************************************************************");
Console.WriteLine();
int[] intarry1 = { ,,,,,,,,};
int[] intarry2 = { ,,,,,,,,,};
var query11 =
from val1 in intarry1
from val2 in intarry2
where val2 % val1 ==
group val2 by val1;
foreach(var grp in query11)
{
Console.Write("{0}: ",grp.Key);
foreach(var val in grp)
{
Console.Write("{0} ",val);
}
Console.WriteLine();
} Console.WriteLine();
Console.WriteLine("*********************join*****************************************");
Console.WriteLine();
Console.WriteLine("**********内部联接**************************");
int[] intarray1 = { ,,,,,};
int[] intarray2 = { ,,,,,,,,,};
var query12 =
from val1 in intarray1
join val2 in intarray2 on val1 % equals val2 %
select new { VAL1 = val1,VAL2 = val2};
foreach(var val in query12)
{
Console.WriteLine(val);
} Console.WriteLine();
Console.WriteLine("**********分组联接**************************"); var query13 =
from val1 in intarray1
join val2 in intarray2 on val1 % equals val2 % into grpName
select new { VAL1 = val1, VAL2 = grpName};
foreach(var val in query13)
{
Console.Write("{0}: ",val.VAL1);
foreach(var obj in val.VAL2)
{
Console.Write("{0} ",obj);
}
Console.WriteLine();
} Console.WriteLine();
Console.WriteLine("**********左外部联接**************************");
var query14 =
from val1 in intarray1
join val2 in intarray2 on val1 % equals val2 % into grpName
from grp in grpName.DefaultIfEmpty()
select new { VAL1 = val1 , VAL2 = grp};
foreach(var val in query14)
{
Console.WriteLine(val);
}
}
}
}

C# LINQ 基本操作实例的更多相关文章

  1. 一篇文章教你学会ASP.Net Core LINQ基本操作

    一篇文章教你学会ASP.Net Core LINQ基本操作 为什么要使用LINQ LINQ中提供了很多集合的扩展方法,配合lambda能简化数据处理. 例如我们想要找出一个IEnumerable< ...

  2. linq基本操作

    一.Linq有两种语法: 1.  方法语法 2.  查询语法 下面举个例子看看这两种方法的区别 比如现在有一个学生类 public class student { public string user ...

  3. Linq 基本操作

    在linq中排序方法有: OrderBy()  --对某列升序排序 ThenBy()    --某列升序后对另一列后续升序排序 OrderByDescending() --对某列降序排序 ThenBy ...

  4. Html基本操作实例代码

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  5. linq小实例

    var cus = from u in context.IPPhoneInfo join r in context.Organization on u.OrgStructure equals r.Mi ...

  6. C#中的LINQ

    从自己的印象笔记里面整理出来,排版欠佳.见谅!   1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q=      from c in catego ...

  7. android 教程实例系列

    用户界面部分学起来还真是无处下手哇,总不能一个控件发一篇文吧,略有点费时间啊...这个难道不是边用边学才给力吗..所以我打算从最实用的Button开始下手. 先贴几个链接,好东西: android用户 ...

  8. C++ 双链表基本操作

    上一篇博客主要总结了单向链表,这次再总结一下双向链表. 1.概念 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都 ...

  9. .NETCoreCSharp 中级篇2-3 Linq简介

    .NETCoreCSharp 中级篇2-3 本节内容为Linq及其拓展方法.Linq中表达式树的使用 简介 语言集成查询(LINQ)是一系列直接将查询功能集成到C#语言的技术统称.数据查询历来都表示为 ...

随机推荐

  1. bzoj4160: [Neerc2009]Exclusive Access 2

    Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input 第一行一个数M (1≤M≤100). 接下来M行,每行两个大写 ...

  2. Linux——搭建PHP开发环境第一步:apache

    原文链接:http://www.2cto.com/os/201511/450258.html ##### Apache 编译安装[root@localhost ~]# yum install gcc ...

  3. HIVE json格式数据的处理

    今天要处理一个以json格式存储的数据,想要直接把json的各个项的数据存入HIVE表中. HIVE直接读入json的函数有两个: (1)get_json_object(string json_str ...

  4. java浮点类型计算

    java浮点类型需要采用java.math.*这个工具包,这样的计算结果才是我们想要的.呵呵 import java.math.BigDecimal; import java.text.NumberF ...

  5. 设计模式(三): FACTORY工厂模式 -- 创建型模式

    1.定义 定义一个用于创建对象的接口,让子类决定实例化哪一个类,Factory Method使一个类的实例化延迟到了子类. 2.适用场景 1.第一种情况是对于某个产品,调用者清楚地知道应该使用哪个具体 ...

  6. Linux如何删除非空目录

    这个问题很basic,不过还是困扰了我一段时间.(这里主要讨论的是命令行模式下) 我本来觉得应该使用命令 rmdir 但是发现它无法删除非空的目录. 后来发现了原来应该使用命令 rm -rf 目录名 ...

  7. 项目管理模式之如何去除SVN标记

    原问地址:http://blog.csdn.net/djcken/article/details/7916986      当项目不需要SVN标志的时候,我们一般怎么办哪??可能很多人设置Window ...

  8. AES - Rijndael 算法(二)

    三:Rijndael算法实现(C++版本) /*-------------------- Rijndael round subkeys ---------------------*/u8 roundK ...

  9. cocos2dx lua binding ,cocos2dx 绑定lua测试

    前面2篇分别简单介绍 手动bind C++ 类和lua:http://blog.csdn.net/chenee543216/article/details/12074771 使用tolua++简化工作 ...

  10. Android Toast 提示按两次返回键退出

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceS ...