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. laravel框架——保存用户登陆信息(session)

    public function inlog(Request $request) { //获取表单提交的数据 $input = $request->all(); //根本获取的数据去数据库中查询 ...

  2. 编程规范之 if 语句的简单规则

    原文: http://www.oschina.net/translate/basic-rules-for-code-readability-and-the-if-statement 代码应该是可阅读就 ...

  3. JAVA之序列化A

    package SwingGui.sky.com; import java.io.*; public class GameSaverTest { public static void main(Str ...

  4. SCALA中类的继承

    慢慢的,回想起以前学习JAVA和C#当中的的类的特性了. 感觉大同小异吧... package com.hengheng.scala class OOPInScala { } class Studen ...

  5. Java获取程序或项目路径的常用方法

    在写java程序时不可避免要获取文件的路径,比较常用的方法有: 1 在任意的class里调用: this.getClass().getClassLoader().getResource("/ ...

  6. C#之VS2010开发Web Service

    一:创建web service vs2010软件默认的framework是4.0版本,所以想创建web服务的时候压根看不到web服务应用程序.网上有人说vs2010的web service 跟wcf合 ...

  7. SWFObject: 基于Javascript的Flash媒体版本检测与嵌入模块

    原文地址:http://www.awflasher.com/flash/articles/swfobj.htm SWFObject: 基于Javascript的Flash媒体版本检测与嵌入模块原文:S ...

  8. 获取git的当前分支名称

    git branch | grep \"*\" | awk -F \"_\" '{print $2}'

  9. SRM468 - SRM469(1-250pt, 500pt)

    SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...

  10. linux系统资源信息监控

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...