C#课后小试6

LINQ可以很方便的对数据源进行筛选、排序和分组操作。

格式 :from   ……      例:          from customer(范围变量) in customers(数据源)

where  ……                      where customer.Firstname=="sadas"  (筛选的条件)

select  ……                     select customer     (映射,具体要选的主体或属性)

LINQ查询式可以立刻执行

foreach (Customer customer in result) {……;}

customers[3].FirstName = "Donna";

foreach (Customer customer in result){…;}  rusult将立刻改变

如果不想改变,可以预存变化前的值,可以使用tolist() toarry()

static void Main()

{

List<Customer> customers = CreateCustomerList();

IEnumerable<Customer> result = from customer in customers

where customer.FirstName == "Donna“ select customer;

List<Customer> cachedResult = result.ToList<Customer>();   //存值,需要之前的值就去查之前存的地方。

foreach (Customer customer in cachedResult){…;}

customers[3].FirstName = "Donna";

Console.WriteLine("FirstName == \"Donna\" (take two)");

foreach (Customer customer in cachedResult){…;}

}

连接 join

[data source 1] join [data source 2] on [join condition]

将两个数据源按照一定条件连接在一起

customer in customers join address in addresses on customer.Name equals address.Name;

排序 orderby

var result =from customer in Customers

orderby customer.LastName

select customer;  //升序排Lastname中数据

var 关键字能够代替局部变量的类型

foreach (var ca in result) //

{

Console.WriteLine(string.Format("{0}\nAddress: {1}",

ca.Customer, ca.Address));

}

分组 group

var result = from address in addresses

group address by address.Name;//按照条件 address.Name 分组

foreach (var gp in result)

{

Console.WriteLine("{0}", gp.Key); //~.key代表分组条件,即address.Name

foreach (var a in gp)

Console.WriteLine("\t{0}", a);

}

时间: 2024-08-17 11:02:33

C#课后小试6的相关文章

C#课后小试7

WPF(Windows Presentation Foundation) WPF是用户界面框架,最大的特点是设计与编程分离,使各专业人才更加专注,不用分心. 我们可以通过XML的变形语言--XAML语言来操作. XAML由一些规则(告诉解析器和编译器如何处理XML)和一些关键字组成,但它自己没有任何有意义的元素. 看下列代码 <Window x:Class="HelloWPF.Window1"    xmlns="http://schemas.microsoft.com

c#课后小试4

Interfaces(接口) 这次课程,我们主要学习了C#中的接口的特点及其使用.依我看来,接口与抽象类很像但有所不同,或者说,借口是抽象类的一种升级. interface与abstract class的主要区别:interface可以放置在程序层的任意位置,abstract class只能放在顶端.. 声明接口: public interface IStorable { void Read( ); void Write(object); } 使用接口: public class Documen

C#课后小试8

Stream(数据流) 命名空间:System.IO 我们看一下文件的读写 1.二进制文件 1 //所读取的文件 2 Stream inputStream=File.OpenRead( 3 @"C:\test\source\test1.cs"); 4 //要写入的文件 5 Stream outputStream=File.Openwrite( 6 @"C:\test\source\test1.bak"); 7 8 //创建一个保存字节的缓存 9 byte [] bu

C#课后小试5

1.string相关函数 我分别尝试了compare  substring split函数,如上图示. compare函数会比较输入是两个string,然后返回一个int值显示结果. substring可以读取string里的一部分,(开始索引,索引个数). split可以对string按照给出的条件分割. 2.正则表达式 由元字符和字面值组成,用来更方便的管理字符串. 对于该部分大学习我参考了http://www.wangqi.com/n9250c53.aspx 3.ecpection exp

c#课后小试3

1.静态类 静态类不能实例化. 静态类是密封的(sealed)不能从它派生(derive)类型. 静态类不能包含非静态成员也不能有构造方法. C#中没有全局方法,我们可以用静态类储存一些想用的数据. static class Sclass { 一些我们需要的静态数据 static A() { …… } } 在主函数中,我门可以这样使用 Sclass.A(); 2.析构函数 如果对象要控制非托管(unmanaged)的资源,用完后要显式地(explicitly)释放托管的资源,这时需要析构方法.

问题 1018: C语言程序设计教程(第三版)课后习题6.8

/******************************************************************** @file Main.cpp @date 2017-05-12 @author Zoro_Tiger @brief 问题 1018: C语言程序设计教程(第三版)课后习题6.8 http://www.dotcpp.com/oj/problem1018.html *************************************************

Redis配置以及通过C#访问小试

首先安装一个Ubuntu14.04的虚拟机用来安装Redis.Ubuntu的Unity在虚拟机里面卡爆了,可以通过如下方法安装传统的Gnome界面: sudo aptitude install gnome-session-fallback 安装完成之后可以在登录的地方选择Gnome界面. Redis的编译和安装在Linux下面非常简单,访问redis.io获得稳定版的源代码.现在是redis-2.8.13.tar.gz 下载完成之后,执行如下命令: tar xzf redis-2.8.13.ta

问题 1041: C语言程序设计教程(第三版)课后习题9.8

/******************************************************************** @file Main.cpp @date 2017-05-28 22:02:55 @author Zoro_Tiger @brief 问题 1041: C语言程序设计教程(第三版)课后习题9.8 http://www.dotcpp.com/oj/problem1041.html ****************************************

问题 1040: C语言程序设计教程(第三版)课后习题9.6

/******************************************************************** @file Main.cpp @date 2017-05-28 21:57:02 @author Zoro_Tiger @brief 问题 1040: C语言程序设计教程(第三版)课后习题9.6 http://www.dotcpp.com/oj/problem1040.html ****************************************