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. 关于32位程序在Win7&64位系统中连接Microsoft Excel数据源的问题

    最近在新公司电脑上跑以前的selenium测试框架的时候,抛出了如下的错误 出现的是ODBC Driver问题:[Microsoft][ODBC Driver Manager] Data source ...

  2. jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件

    写词典在线编辑器用到的一个功能 能获取选中的文字.当前的光标的位置 等位置,而且支持多个文本框一起操作 非常方便 git地址:https://github.com/localhost/jquery-f ...

  3. Extjs4 自定义组件

    Ext.onReady (function () { Ext.define ('MydesktopIcon', { /* Begin Definitions */ alias: 'widget.des ...

  4. RR 和RC隔离问题

    Sesssion 1: mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +--------------- ...

  5. -_-#【AJAX】XMLHttpRequest

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. Ubuntu中Samba的安装配置和使用[图文]

    Samba服务在Ubuntu服务器版本中默认并没有安装. 1. Samba软件包的安装 使用源安装,在终端中输入如下命令: #sudo apt-get install samba#sudo apt-g ...

  7. 【动态规划】XMU 1030 苦恼的月下老人

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1030 题目大意: 给定两个字符串的长度和内容,求最长公共子序列. 题目思路: [动态规 ...

  8. CSS padding margin border属性详解【转载】

    本文转载自:http://www.cnblogs.com/linjiqin/p/3556497.html ,感谢相关博主. 图解CSS padding.margin.border属性 W3C组织建议把 ...

  9. hadoop 运行 datanode , mac 系统

    问题描述 今天使用 hadoop 时,发现无法通过下面命令上传文件到 hadoop 文件系统,会报错. bin/hadoop fs -put input . 运行 jps 后,输出如下: Resour ...

  10. IOS调试lldb命令常用po

    lldb命令常用(备忘) 假如你准备在模拟器里面运行这个,你可以在“(lldb)”提示的后面输入下面的: (lldb) po $eax LLDB在xcode4.3或者之后的版本里面是默认的调试器.假如 ...