十四、C# 支持标准查询运算符的集合接口
class Program
{
static void Main(string[] args)
{
var patent1 = new
{
Title = "xxm1",
YearOfPublication = ""
};
var patent2 = new
{
Title = "xxm2",
YearOfPublication = ""
};
var patent3 = new
{
patent1.Title,
//重新命名属性
Year = patent2.YearOfPublication
}; Console.WriteLine(patent1.Title + ":" + patent1.YearOfPublication);
Console.WriteLine(patent2.Title + ":" + patent2.YearOfPublication);
Console.WriteLine(patent3.Title + ":" + patent3.Year); Console.WriteLine();
Console.WriteLine(patent1);
Console.WriteLine(patent2);
Console.WriteLine(patent3); Console.ReadLine(); }
} 输出:
xxm1:
xxm2:
xxm1: { Title = xxm1, YearOfPublication = }
{ Title = xxm2, YearOfPublication = }
{ Title = xxm1, Year = }
string text=" this is a test of the ...";
//<====>
var text="this is a test of the ...";
var patent3 = new
{
patent1.Title,
//重新命名属性
Year = patent2.YearOfPublication
};
class Program
{
static void Main(string[] args)
{ List<string> sevenWorldBlunders = new List<string>();
sevenWorldBlunders = new List<string>()
{
"Wealth without work",
"Pleasure without conscience",
"Knowledge without character",
}; Print(sevenWorldBlunders); }
private static void Print<T>(IEnumerable<T> items)
{
foreach (T item in items)
{
Console.WriteLine(item);
} }
}
int[] arr = new[] { , , , , }; foreach (int item in arr)
{
Console.WriteLine(item);
}
class Program
{
static void Main(string[] args)
{
IEnumerable<Patent> patents = PatentData.Patents; Print(patents); Console.WriteLine(); IEnumerable<Inventor> inventors = PatentData.Inventors; Print(inventors); Console.ReadLine(); }
public static void Print<T>(IEnumerable<T> items)
{
foreach (T item in items)
{
Console.WriteLine(item);
}
}
//专利类
public class Patent
{
public string Title { get; set; } public string YearOfPublication { get; set; }
public string ApplicationNumber { get; set; }
public long[] InventorIds { get; set; }
public override string ToString()
{
return string.Format("{0}({1})", Title, YearOfPublication);
}
}
//发明者类
public class Inventor
{
public long Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public override string ToString()
{
return string.Format("{0}({1},{2})", Name, City, State);
}
} //实际数据
public static class PatentData
{
public static readonly Inventor[] Inventors = new Inventor[] {
new Inventor(){
Name="Benjamin Franklin",City="Philadelphia",
State="PA",Country="USA",Id=
},
new Inventor(){
Name="Orville Wright",City="Kitty Hawk",
State="NC",Country="USA",Id=
},
new Inventor(){
Name="Wilbur Wright",City="Kitty Hawk",
State="NC",Country="USA",Id=
},
new Inventor(){
Name="Samuel Morse",City="New York",
State="NY",Country="USA",Id=
},
new Inventor(){
Name="George Stephenson",City="Wylam",
State="Northumberland",Country="UK",Id=
},
new Inventor(){
Name="John Michaelis",City="Chicago",
State="IL",Country="USA",Id=
},
new Inventor(){
Name="Mary Phelps Jacob",City="New York",
State="NY",Country="USA",Id=
},
}; public static readonly Patent[] Patents = new Patent[] {
new Patent(){
Title="Bifocals",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Phonograph",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Kinetoscope",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Electrical Telegraph",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Flying machine",YearOfPublication="",
InventorIds=new long[]{,}
},
new Patent(){
Title="Steam Locomotive",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Droplet deposition apparatus",YearOfPublication="",
InventorIds=new long[]{}
},
new Patent(){
Title="Backless Brassiere",YearOfPublication="",
InventorIds=new long[]{}
},
};
}
}
IEnumerable<Patent> patents = PatentData.Patents; patents = patents.Where(
patent => patent.YearOfPublication.StartsWith("")
); Print(patents);
IEnumerable<Patent> patents = PatentData.Patents; IEnumerable<Patent> patents1800 = patents.Where(
patent => patent.YearOfPublication.StartsWith("")
);
IEnumerable<string> items = patents1800.Select(item => item.ToString()); //Print(patents);
Print(items);
IEnumerable<string> filelist = Directory.GetFiles("D:\\");
IEnumerable<FileInfo> files = filelist.Select(file => new FileInfo(file));
Print(files);
//注:以上的Lambda表达的形式参数的类型都与集合中的元素类型一致。
匿名类型:
IEnumerable<string> filelist = Directory.GetFiles("D:\\");
var items = filelist.Select(file =>
{
FileInfo fileInfo = new FileInfo(file);
return new { FileName = fileInfo.Name, Size = fileInfo.Length };
});
Print(items);
IEnumerable<string> filelist = Directory.GetFiles("D:\\");
var items = filelist.AsParallel().Select(file =>
{
FileInfo fileInfo = new FileInfo(file);
return new { FileName = fileInfo.Name, Size = fileInfo.Length };
});
Print(items);
IEnumerable<Patent> patents = PatentData.Patents;
Console.WriteLine("Patent Count:{0}", patents.Count());
Console.WriteLine("Patent Count in 1800s:{0}",
patents.Count(
patent => patent.YearOfPublication.StartsWith("")
));
if(patents.Any())
{ }
IEnumerable<Patent> patents = PatentData.Patents;
bool result;
patents = patents.Where(patent =>
{
if (result = patent.YearOfPublication.StartsWith(""))
{
Console.WriteLine("Where StartsWith 18 :" + patent);
}
return result;
});
Console.WriteLine("1. Patents prior to the 1900s are:");
foreach (Patent patent in patents)
{ }
Console.WriteLine();
Console.WriteLine("2. A second listing of patents prior to the 1900s:");
Console.WriteLine(" There are {0} patents prior to 1900.", patents.Count()); Console.WriteLine();
Console.WriteLine("3. A third listing of patents prior to the 1900s:");
patents = patents.ToArray();
Console.Write(" There are ");
Console.WriteLine("{0} patents prior to 1900.", patents.Count());
IEnumerable<Patent> items = null;
Patent[] patents = PatentData.Patents; items = patents.OrderBy(patent => patent.YearOfPublication)
.ThenBy(patent => patent.Title);
Print(items); Console.WriteLine(); items = patents.OrderByDescending(patent => patent.YearOfPublication)
.ThenByDescending(patent => patent.Title);
Print(items);
class Program
{
static void Main(string[] args)
{ Department[] departments = CorporateData.Departments;
Employee[] employees = CorporateData.Employees; var items = employees.Join(
departments,//关联的数据
employee => employee.DepartmentId,//联接时的参考属性
department => department.Id,//联接时的参考属性
(employee, department) => new //返回的数据
{
employee.Id,
employee.Name,
employee.Title,
Department = department });
Print(items); Console.ReadLine(); }
public static void Print<T>(IEnumerable<T> items)
{
foreach (T item in items)
{
Console.WriteLine(item);
}
} //部门类
public class Department
{
public long Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("{0}", Name);
}
} //员工类
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public int DepartmentId { get; set; }
public override string ToString()
{
return string.Format("{0}({1})", Name, Title);
}
} //实际数据
public static class CorporateData
{
public static readonly Department[] Departments = new Department[] {
new Department()
{
Name="Corporate",Id=
},
new Department()
{
Name="Corporate",Id=
},
new Department()
{
Name="Engineering",Id=
},
new Department()
{
Name="Information Technology",Id=
},
new Department()
{
Name="Research",Id=
},
new Department()
{
Name="Marketing",Id=
},
};
public static readonly Employee[] Employees = new Employee[] {
new Employee()
{
Name="Mark Michaelis",
Title="Chief Computer Nerd",
DepartmentId=
},
new Employee()
{
Name="Michael Stokesbary",
Title="Senior Computer Wizard",
DepartmentId=
},
new Employee()
{
Name="Brian Jones",
Title="Enterprise Integration Guru",
DepartmentId=
},
new Employee()
{
Name="Jewel Floch",
Title="Bookkeeper Extraordinaire",
DepartmentId=
},
new Employee()
{
Name="Robert Stokesbary",
Title="Expert Mainframe Engineer",
DepartmentId=
},
new Employee()
{
Name="Paul R.Bramsman",
Title="Programmer Extraordinaire",
DepartmentId=
},
new Employee()
{
Name="Thomas Heavey",
Title="Software Architect",
DepartmentId=
},
new Employee()
{
Name="John Michaelis",
Title="Inventor",
DepartmentId=
}, };
} } 输出:
{ Id = , Name = Mark Michaelis, Title = Chief Computer Nerd, Department = Corpo
rate }
{ Id = , Name = Michael Stokesbary, Title = Senior Computer Wizard, Department
= Engineering }
{ Id = , Name = Brian Jones, Title = Enterprise Integration Guru, Department =
Engineering }
{ Id = , Name = Jewel Floch, Title = Bookkeeper Extraordinaire, Department = Co
rporate }
{ Id = , Name = Robert Stokesbary, Title = Expert Mainframe Engineer, Departmen
t = Information Technology }
{ Id = , Name = Paul R.Bramsman, Title = Programmer Extraordinaire, Department
= Engineering }
{ Id = , Name = Thomas Heavey, Title = Software Architect, Department = Enginee
ring }
{ Id = , Name = John Michaelis, Title = Inventor, Department = Research }
IEnumerable<Employee> employees = CorporateData.Employees;
IEnumerable<Department> departments = CorporateData.Departments; IEnumerable<IGrouping<int, Employee>> groupedEmployees =
employees.GroupBy(employee => employee.DepartmentId);
foreach (IGrouping<int, Employee> employeeGroup in groupedEmployees)
{
Console.WriteLine();
foreach (Employee employee in employeeGroup)
{
Console.WriteLine("\t"+employee);
}
Console.WriteLine("\t Count:"+employeeGroup.Count());
}
IEnumerable<Employee> employees = CorporateData.Employees;
IEnumerable<Department> departments = CorporateData.Departments; var items = departments.GroupJoin(
employees,//关联的对象数组
department => department.Id,//关联的键
employee => employee.DepartmentId,//关联的键
(department, departmentEmployees) => new//返回的数据
{
department.Id,
department.Name,
Employees = departmentEmployees
});
foreach (var item in items)
{
Console.WriteLine("部门:{0}", item.Name+":");
foreach (Employee employee in item.Employees)
{
Console.WriteLine("\t"+employee);
}
}
string[] text = { "a b", "c d", "e f" };
var items = text.Select(
item => item.Split(' ')
);
foreach (string[] strs in items)
{
foreach (string str in strs)
{
Console.WriteLine("\t"+str);
}
}
换成SelectMany
string[] text = { "a b", "c d", "e f" };
var items = text.SelectMany(
item => item.Split(' ')
);
foreach (var item in items)
{
Console.WriteLine("\t" + item);
}
十四、C# 支持标准查询运算符的集合接口的更多相关文章
- 《C#本质论》读书笔记(14)支持标准查询操作符的集合接口
14.2.集合初始化器 使用集合初始化器,程序员可以采用和数组相似的方式,在集合的实例化期间用一套初始的成员来构造这个集合. 如果没有集合初始化器,就只有在集合实例化后才能显示添加到集合中--例如 ...
- C#3.0新增功能09 LINQ 标准查询运算符 01 概述
连载目录 [已更新最新开发文章,点击查看详细] 标准查询运算符 是组成 LINQ 模式的方法. 这些方法中的大多数都作用于序列:其中序列指其类型实现 IEnumerable<T> 接 ...
- .NET中那些所谓的新语法之四:标准查询运算符与LINQ
开篇:在上一篇中,我们了解了预定义委托与Lambda表达式等所谓的新语法,这一篇我们继续征程,看看标准查询运算符和LINQ.标准查询运算符是定义在System.Linq.Enumerable类中的50 ...
- SQO (标准查询运算符)方法 & Linq To Object
#region SQO (标准查询运算符) 方法 #region Where() Find() FindAll() FirstOrDefault()等方法 static void c01where() ...
- C#3.0新增功能09 LINQ 标准查询运算符 04 运算
连载目录 [已更新最新开发文章,点击查看详细] 本篇主要介绍标准查询运算符的常用运算功能. 01 对数据排序 排序操作基于一个或多个属性对序列的元素进行排序. 第一个排序条件对元素执行主要排序. ...
- .NET LINQ标准查询运算符
标准查询运算符概述 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法. 大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了 IEnumerable<T> ...
- MVC ---- 标准查询运算符
标准查询运算符:定义在System.Linq.Enumerable类中的50多个为IEnumerable<T>准备的扩展方法,这些方法用来 对它操作的集合进行查询筛选. 筛选集合Where ...
- “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法
“标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> 接口或 IQueryable& ...
- C#3.0新增功能09 LINQ 标准查询运算符 02 查询表达式语法
连载目录 [已更新最新开发文章,点击查看详细] 某些使用更频繁的标准查询运算符具有专用的 C# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...
随机推荐
- 【HDOJ】1823 Luck and Love
二维线段树.wa了几次,不存在输出-1,而不再是一位小数. #include <cstdio> #include <cstring> #define MAXN 105 #def ...
- 通过jquery-ui中的sortable来实现拖拽排序
1.引入文件 <script src="{sh::PUB}js/jquery-1.10.1.min.js"></script> <link rel=& ...
- poj3519
凡是差分约束系统的题目都是转化为d[j]-d[i]<=w[i,j]的形式然后我们建立边i-->j 边权为w[i,j]对于这道题,要求d[n]-d[1]尽可能的大设d[i]为相对差,d[1] ...
- ARM 开发板嵌入式linux系统与主机PC通过串口传输文件
本文转载自http://useless20.blog.163.com/blog/static/237409982010227127576/ 嵌入式linux系统与主机通过串口传输文件 我想如果要从PC ...
- 网络流(最大流):CodeForces 499E Array and Operations
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m goo ...
- HDOJ(HDU) 2523 SORT AGAIN(推导排序、、)
Problem Description 给你N个整数,x1,x2-xn,任取两个整数组合得到|xi-xj|,(0 < i,j<=N,i!=j). 现在请你计算第K大的组合数是哪个(一个组合 ...
- 怎么把GPUImageFIlter处理过的图像保存成UIImage
总共有两种方法能够把GPUImage处理过的图片转化成UIImage 方法一: UIImage *inputImage = [UIImage imageNamed:@"Lambeau ...
- OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(三)——计算节点的安装
序:OpenStack Havana 部署在Ubuntu 12.04 Server [OVS+GRE] 计算节点: 1.准备结点 安装好ubuntu 12.04 Server 64bits后,进入ro ...
- 《Linear Algebra and Its Applications》-chaper4-向量空间-子空间、零空间、列空间
在线性代数中一个非常重要的概念就是向量空间R^n,这一章节将主要讨论向量空间的一系列性质. 一个向量空间是一些向量元素构成的非空集合V,需要满足如下公理: 向量空间V的子空间H需要满足如下三个条件: ...
- MongoDB性能优化指南
一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...