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. asp.net webform 局部发布更新

    一:关于webform编译 编译时会将每个aspx文件单独生成dll文件于bin目录下.也会将引用的dll存放于bin目录 二:对界面或者引用的dll(如BLL层,DAL层等)做了修改更新后在服务器只 ...

  2. tomcat正常启动,但IP不能访问web。ping IP地址,一直超时。 用ipconfig命令修复TCP/IP的配置信息

    今天遇到一个好奇葩的问题  好吧是昨天遇到的一直没找到解决办法(`へ´) tomcat正常启动,但是通过IP不能访问web 用IP地址就是不行  (:′⌒`)  打不开 localhost就可以    ...

  3. SQLSERVER一个比较不错的分页存储过程p_splitpage

    CREATE procedure p_splitpage @sql nvarchar(4000), --要执行的sql语句 @page int=1, --要显示的页码 @pageSize int, - ...

  4. 转:CentOS6.3配置yum源

    全新以最小化包安装了64位的CentOS6.3系统,作为本地的Web服务器使用,现记录全过程第二步,配置网易163的yum源 1. 下载repo文件    下载地址:http://mirrors.16 ...

  5. python里的Join函数

    用法是将数组里的每个元素用字符连接起来 import string string.join(["aaaa", "bbb"]) 或者: from string i ...

  6. Linux&shell之结构化命令

    写在前面:案例.常用.归类.解释说明.(By Jim)使用if-then语句如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令.如果命令的退出状态是0以外的其他值,那么then后面的 ...

  7. HDOJ(HDU) 1718 Rank(水题、、、)

    Problem Description Jackson wants to know his rank in the class. The professor has posted a list of ...

  8. 尚学堂 JAVA DAY11 概念总结

    1.冒泡排序算法 <升序排列> 思路1: 数组长度为n,那么要对数组进行n-1次遍历; step 1---从数组的第一个数开始,两两比较,如果第一个数 > 第二个数,将二者进行交换: ...

  9. Hbase 设计与开发实战

    Hbase 概述 大数据及 NoSQL 的前世今生 传统的关系型数据库处理方式是基于全面的 ACID 保证,遵循 SQL92 的标准表设计模式(范式)和数据类型,基于 SQL 语言的 DML 数据交互 ...

  10. Q - Tour - hdu 3488(最小匹配值)

    题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...