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. c语言Winpcap编程构造并接收解析arp包

    /* 程序功能: 1.构造arp包,并发送.程序参数顺序:源IP.目的IP.mac地址.flag 2.获取网络中的ARP数据包,解析数据包的内容.程序参数:日志文件名 winpacp中文技术文档(基本 ...

  2. poj1159 Palindrome

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  3. HDU1276(士兵队列训练模拟与链表)

    HDU1276 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descripti ...

  4. Azure IoT

    微软Azure IoT   国外物联网平台初探(二)——微软Azure IoT 马智 平台定位 连接设备.其它 M2M 资产和人员,以便在业务和操作中更好地利用数据. 连接 IoT 设备 将所有设备连 ...

  5. Web Services 介绍

    Web Services 介绍 Web Services 是建立可交互操作的分布式应用程序的新平台 ; Web Services 平台是一套标准,它定义了应用程序如何在 Web 上进行交互操作 , 你 ...

  6. JavaScript Web Application summary

    Widget/ HTML DOM (CORE) (local dom) DOM, BOM, Event(Framework, UI, Widget) function(closure) DATA (c ...

  7. Qt_chartdirector图形开发

    ChartDirector 是一款商业的图表库,有多种语言的版本,使用它做的图表非常的精 细漂亮,提供免费版本,但会出现logo信息.网上有很多关于它的破解方法. 一.产品优点 高效快捷 采用多线程结 ...

  8. linux下文件编码的查看与转换(转)

    通常来说,Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8,所以Linux下打开windows的文件会有乱码的情况.另外,有时要将文件进行编码转换,如将简体中文转 ...

  9. MySQL全连接(Full Join)实现,union和union all用法

    MySQL本身不支持你所说的full join(全连接),但可以通过union来实现 ,下面是一个简单测试,可以看看: mysql> CREATE TABLE a(id int,name cha ...

  10. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...