十四、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# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...
随机推荐
- 【转】Android 驱动开发系列四
原文网址:http://www.2cto.com/kf/201304/202040.html 时隔多日,终于都抽出时间来写blog了.废话不多说,接着上一篇,这里将介绍如何编写HAL层(硬件抽象层)对 ...
- android 自动化(1)
学习android自动化测试要感谢一个朋友耐心的指导 环境搭建:(需要java JDK 以及android SDK) JDK:http://www.oracle.com/technetwork/jav ...
- Different Ways to Add Parentheses——Leetcode
Given a string of numbers and operators, return all possible results from computing all the differen ...
- HDOJ 2012 素数判定
Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x < y<=50),判定该表达式的值是否都为素数. I ...
- cocos2d的ARC开启
ARC,官方解释是Automatic Reference Counting,是Apple公司从iOS5开始为开发者新添加的一个功能. 相信很多写移动开发,可能不只是移动开发的人都深有体会,创建一个对象 ...
- win7 变WIFI热点 & 在线Linux 内核代码
https://daniel.haxx.se/docs/sshproxy.html netsh wlan set hostednetwork mode=allow ssid=mywifi key=1 ...
- 关于字符编码精简介绍 ANSI GB2312 UTF8 UNICODE
- SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏
数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...
- iOS开发总结-类似京东分类,UICollectionView
// // TypeViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 com.ws ...
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
在用opencv处理图片显示在qt label上的时候遇到不是问题 1. qt上要用qimage形式才干显示 IplImage转成 Qimage 彩色图像转换 IplImage *fram; QIm ...