十四、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# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...
随机推荐
- PL/SQL 异常处理
SQL> set serveroutput on SQL> declare name varchar2(10); begin select ename into name from ...
- WordPress /wp-admin/includes/post.php user_ID 参数操作权限提升漏洞
漏洞版本: WordPress 3.6 漏洞描述: Bugtraq ID:62346 CVE ID:CVE-2013-4340 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PH ...
- BZOJ1603: [Usaco2008 Oct]打谷机
1603: [Usaco2008 Oct]打谷机 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 602 Solved: 458[Submit][Stat ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- TFS 2012使用简介(一)
最近,一直想让团队加入敏捷开发,但TFS2010用下来,并不是令人满意,还好,TFS2012横空出世了.相比TFS2010,TFS2012改进太多了,主要体现在以下方面: Team Web Acces ...
- unity3d 雪与沙的渲染
很简单的一个shader,跟着上一篇地形顺便弄的 方法就是基于物理的diffuse,再加上noise权重的specular 公式参考 JOURNEY JOURNEY中认为OrenNayar比较浪费,所 ...
- yarn安装部署
在HDFS HA(http://www.cnblogs.com/yinchengzhe/p/5140117.html)基础上进行yarn的安装. 1.配置yarn-site.xml 参数详情参考 ht ...
- java---Unicode-字符转换器
实现一个字符(包括汉字)的简单互相转换: package cn.hncu.gui2; import java.awt.Button; import java.awt.Color; import jav ...
- [JIT_APP]Android SQLite简介
SQLite介绍 SQLite是一个非常流行的嵌入式数据库,它支持SQL语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目(Mozilla, PHP, Pyt ...
- HDU 4432 Sum of divisors (进制模拟)
三个小函数 getdiv(); 求因子 getsum(); 求平方和 change(); 转换成该进制 #include <cstdio> #include ...