A.今天花了差不多4个小时学习LINQ to Objects,学到了很多东西,以下是学习笔记:
从根本上说,LINQ to Objects 表示一种新的处理集合的方法,采用旧方法,必须编写指定如何从集合检索数据的复杂的 foreach 循环。而采用 LINQ 方法,只需编写描述要检索的内容的声明性代码 ,与传统的 foreach 循环相比,LINQ 查询具有三大优势:
– 更简明、更易读,尤其在筛选多个条件时。
– 使用最少的应用程序代码提供强大的筛选、排序和分组功能。
通常,对数据执行的操作越复杂,使用 LINQ 代替传统迭代技术的好处就越多
//字符串中某个单词出现次数查询
string text ="Kingsoft Co., Ltd. entered software industry in 1988.";
string[] source = text.Split(new char[] { ',','!', '.', '?', ';', ':',' '});
string searchwords = "in";
var s = from t in source
where t.ToLowerInvariant() == searchwords.ToLowerInvariant()
select t;
int count = s.Count();
Label1.Text = count.ToString();
//查询字符串中的数字
string text1 = "ASDF65756E5-5D4D5DES";
var s1 = from t in text1
where Char.IsDigit(t)
select t;
foreach (var ch in s1) {
Label1.Text += ch.ToString();
}
//使用正则表达式操作文件内容
string path = @"J:/编程软件/VS2008";
IEnumerable<System.IO.FileInfo> filelist = GetFiles(path);//读取文件夹内所有文件
System.Text.RegularExpressions.Regex searchTerm =
new System.Text.RegularExpressions.Regex(@"Visual {Basic|C#|C/+/+|J#|SourceSafe|Studio}");//设定查询规则
var searchT = from file in filelist
where file.Extension == ".htm"//查询以htm结尾的文件
let fileText = System.IO.File.ReadAllText(file.FullName)//将GetFiles方法返回的文件,存入fileText中
let rule = searchTerm.Matches(fileText)//按照查询规则进行筛选
where rule.Count > 0//如果存在符合条件的选项
select new//进行赋值
{
name = file.FullName,
rules = from System.Text.RegularExpressions.Match rulese in rule
select rulese
};
foreach (var ts in searchT) {
Label1.Text+= ts.name.Substring(path.Length-1)+"<br>";
foreach (var ts2 in ts.rules) {
Label2.Text += " " + ts2 + "<br>";
}
}
/*反射:其实反射是这样描述的:程序集包含模块,而模块包含类型,类型又包含成员。
* 反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,
* 将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
* 依据个人的肤浅了解,反射机制的采用在很大程度上依赖于.net 框架里面的metadata元数据,
* 在受控代码编译以后生成的是metadata来描述元数据,所有的什么class,Delegate,Event ,interface,
* 等这些东西都会加载相应的metadata到内存里面。当然,这些东西是存储在一个CLR-based编译器
* 产生的module 里面的,这个东东加载就可以读到里面的东西,其优势是程序不加载也可以。
* 可以把这些东西存到文件里面读出来(当让大多数时候还是程序编译的时候读的)。
* 当我们读取metadata的过程,就是反射。
下面的例子介绍反射
*/
string filePath = @"C:/Program Files/Reference Assemblies/Microsoft/Framework/v3.5/System.Core.dll";
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(filePath);
var y = from type in assembly.GetTypes()
where type.IsPublic
from method in type.GetMethods()
where method.ReturnType.IsArray == true
group method.ToString() by type.ToString();
foreach (var ts in y)
{
Label1.Text += ts.Key + "<br>";
foreach (var ts2 in ts)
{
Label2.Text += " " + ts2 + "<br>";
}
}
//linq一般是延迟执行,下面进行一个立即执行的例子
string Lastpath = @"J:/编程软件/VS2008";
IEnumerable<System.IO.FileInfo> filelist1 = GetFiles(path);//读取文件夹内所有文件
var yan = (from file in filelist1
orderby file.CreationTime
select new { file.FullName, file.CreationTime }).Last();
string tr = yan.FullName + " " + yan.CreationTime;
//arraylist数组使用
System.Collections.ArrayList arr=new System.Collections.ArrayList();
arr.Add(new Student { LastName = "Omelchenko", Scores = new List<int> { 97, 92, 81, 60 } } );
arr.Add(new Student { LastName = "O'Donnell", Scores = new List<int> { 75, 84, 91, 39 } });
arr.Add( new Student { LastName = "Mortensen", Scores = new List<int> { 88, 94, 65, 85 } });
arr.Add(new Student { LastName = "Garcia", Scores = new List<int> { 97, 89, 85, 82 } });
arr.Add(new Student { LastName = "Beebe", Scores = new List<int> { 35, 72, 91, 70 } });
var query = from Student student in arr
//注意:使用非泛型Ienumerable集合时,必须显式声明范围变量的类型:如Student student
where student.Scores[0]>90
select student;
foreach (var qu in query) {
string terere = qu.LastName + ":" + qu.Scores[0];
}
//读取文件的方法,传入一个路径就行
static IEnumerable<System.IO.FileInfo> GetFiles(string Path) {
if (!System.IO.Directory.Exists(Path)) {
throw new System.IO.DirectoryNotFoundException();
}
string[] filenames = null;
List<System.IO.FileInfo> files=new List<System.IO.FileInfo>();
filenames=System.IO.Directory.GetFiles(Path,"*.*",System.IO.SearchOption.AllDirectories);
foreach (string name in filenames) {
files.Add(new System.IO.FileInfo(name));
}
return files;
}
public class Student
{
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
以上是学习代码,最后还有查询语法和方法语法
查询语法:
var numQuery1=from num in numbers
where num % 2 = = 0
orderby num
方法语法:其实就是Lambda表达式:
var numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
B:今天的Photo内容很少就只有标尺、度量、还有参考线,都很简单
发两张我做的图吧!呵呵
我的太极图
我的百叶窗
都很简单,呵呵,今天真轻松!
从根本上说,LINQ to Objects 表示一种新的处理集合的方法,采用旧方法,必须编写指定如何从集合检索数据的复杂的 foreach 循环。而采用 LINQ 方法,只需编写描述要检索的内容的声明性代码 ,与传统的 foreach 循环相比,LINQ 查询具有三大优势:
– 更简明、更易读,尤其在筛选多个条件时。
– 使用最少的应用程序代码提供强大的筛选、排序和分组功能。
通常,对数据执行的操作越复杂,使用 LINQ 代替传统迭代技术的好处就越多
//字符串中某个单词出现次数查询
string text ="Kingsoft Co., Ltd. entered software industry in 1988.";
string[] source = text.Split(new char[] { ',','!', '.', '?', ';', ':',' '});
string searchwords = "in";
var s = from t in source
where t.ToLowerInvariant() == searchwords.ToLowerInvariant()
select t;
int count = s.Count();
Label1.Text = count.ToString();
//查询字符串中的数字
string text1 = "ASDF65756E5-5D4D5DES";
var s1 = from t in text1
where Char.IsDigit(t)
select t;
foreach (var ch in s1) {
Label1.Text += ch.ToString();
}
//使用正则表达式操作文件内容
string path = @"J:/编程软件/VS2008";
IEnumerable<System.IO.FileInfo> filelist = GetFiles(path);//读取文件夹内所有文件
System.Text.RegularExpressions.Regex searchTerm =
new System.Text.RegularExpressions.Regex(@"Visual {Basic|C#|C/+/+|J#|SourceSafe|Studio}");//设定查询规则
var searchT = from file in filelist
where file.Extension == ".htm"//查询以htm结尾的文件
let fileText = System.IO.File.ReadAllText(file.FullName)//将GetFiles方法返回的文件,存入fileText中
let rule = searchTerm.Matches(fileText)//按照查询规则进行筛选
where rule.Count > 0//如果存在符合条件的选项
select new//进行赋值
{
name = file.FullName,
rules = from System.Text.RegularExpressions.Match rulese in rule
select rulese
};
foreach (var ts in searchT) {
Label1.Text+= ts.name.Substring(path.Length-1)+"<br>";
foreach (var ts2 in ts.rules) {
Label2.Text += " " + ts2 + "<br>";
}
}
/*反射:其实反射是这样描述的:程序集包含模块,而模块包含类型,类型又包含成员。
* 反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,
* 将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
* 依据个人的肤浅了解,反射机制的采用在很大程度上依赖于.net 框架里面的metadata元数据,
* 在受控代码编译以后生成的是metadata来描述元数据,所有的什么class,Delegate,Event ,interface,
* 等这些东西都会加载相应的metadata到内存里面。当然,这些东西是存储在一个CLR-based编译器
* 产生的module 里面的,这个东东加载就可以读到里面的东西,其优势是程序不加载也可以。
* 可以把这些东西存到文件里面读出来(当让大多数时候还是程序编译的时候读的)。
* 当我们读取metadata的过程,就是反射。
下面的例子介绍反射
*/
string filePath = @"C:/Program Files/Reference Assemblies/Microsoft/Framework/v3.5/System.Core.dll";
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(filePath);
var y = from type in assembly.GetTypes()
where type.IsPublic
from method in type.GetMethods()
where method.ReturnType.IsArray == true
group method.ToString() by type.ToString();
foreach (var ts in y)
{
Label1.Text += ts.Key + "<br>";
foreach (var ts2 in ts)
{
Label2.Text += " " + ts2 + "<br>";
}
}
//linq一般是延迟执行,下面进行一个立即执行的例子
string Lastpath = @"J:/编程软件/VS2008";
IEnumerable<System.IO.FileInfo> filelist1 = GetFiles(path);//读取文件夹内所有文件
var yan = (from file in filelist1
orderby file.CreationTime
select new { file.FullName, file.CreationTime }).Last();
string tr = yan.FullName + " " + yan.CreationTime;
//arraylist数组使用
System.Collections.ArrayList arr=new System.Collections.ArrayList();
arr.Add(new Student { LastName = "Omelchenko", Scores = new List<int> { 97, 92, 81, 60 } } );
arr.Add(new Student { LastName = "O'Donnell", Scores = new List<int> { 75, 84, 91, 39 } });
arr.Add( new Student { LastName = "Mortensen", Scores = new List<int> { 88, 94, 65, 85 } });
arr.Add(new Student { LastName = "Garcia", Scores = new List<int> { 97, 89, 85, 82 } });
arr.Add(new Student { LastName = "Beebe", Scores = new List<int> { 35, 72, 91, 70 } });
var query = from Student student in arr
//注意:使用非泛型Ienumerable集合时,必须显式声明范围变量的类型:如Student student
where student.Scores[0]>90
select student;
foreach (var qu in query) {
string terere = qu.LastName + ":" + qu.Scores[0];
}
//读取文件的方法,传入一个路径就行
static IEnumerable<System.IO.FileInfo> GetFiles(string Path) {
if (!System.IO.Directory.Exists(Path)) {
throw new System.IO.DirectoryNotFoundException();
}
string[] filenames = null;
List<System.IO.FileInfo> files=new List<System.IO.FileInfo>();
filenames=System.IO.Directory.GetFiles(Path,"*.*",System.IO.SearchOption.AllDirectories);
foreach (string name in filenames) {
files.Add(new System.IO.FileInfo(name));
}
return files;
}
public class Student
{
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
以上是学习代码,最后还有查询语法和方法语法
查询语法:
var numQuery1=from num in numbers
where num % 2 = = 0
orderby num
方法语法:其实就是Lambda表达式:
var numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
B:今天的Photo内容很少就只有标尺、度量、还有参考线,都很简单
发两张我做的图吧!呵呵
我的太极图

我的百叶窗

都很简单,呵呵,今天真轻松!