飘遥的Blog

-专注于.NET开发
posts - 83, comments - 46, trackbacks - 8, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2008年10月6日

     摘要: Mono是一个多平台支持的开源的.NET framework的实现,支持的操作系统有Unix, Windows, MacOS 和一些其他操作系统。 Mono 2.0包括ADO.NET 2.0/ASP.NET 2.0/Windows.Forms 2.0/System.XML 2.0/System.Drawing/Linq/GTK#等类库的实现。还包括C# 3.0/VB 8/IL编译器和很丰富的工具,如mono平台上的开发工具monodevelop等。  阅读全文

posted @ 2008-10-06 14:55 飘遥(周振兴) 阅读(148) | 评论 (0)编辑

2008年9月28日

     摘要: FCKEditor 2.6.3版早已发布,新版本的FCKEditor生成的代码规范,符合XHTML标准;用拖动层模拟弹出窗口,不会被拦截,使用更方便。更多新特性参考:http://www.fckeditor.net/whatsnew 代码高亮组件使用最新版的ActiproSoftware.CodeHighlighter 。  阅读全文

posted @ 2008-09-28 15:05 飘遥(周振兴) 阅读(278) | 评论 (4)编辑

2008年9月18日

     摘要: 上一篇《彻底研究String》介绍了String类型的一些性质。.NET对String的优化,能高效安全的完成一些操作,但正是这些优化导致了在进行某些操作时会占用大量的资源,如拼接字符串、修改字符串等等,高效地完成这些操作的替代类型是StringBuilder。  阅读全文

posted @ 2008-09-18 13:24 飘遥(周振兴) 阅读(2452) | 评论 (8)编辑

2008年9月15日

     摘要: String是很常用的类型,但有的同学在使用过程中存在一些误区,导致效率低下,在此对其机制进行一个彻底的讨论,水平有限,如有不同的见解请留言讨论。  阅读全文

posted @ 2008-09-15 22:45 飘遥(周振兴) 阅读(3236) | 评论 (24)编辑

2008年9月13日

     摘要: C#调用非托管程序有多种方式,这里整理一下。  阅读全文

posted @ 2008-09-13 18:31 飘遥(周振兴) 阅读(163) | 评论 (0)编辑

     摘要: 本篇介绍Linq的延迟执行和查询非泛型集合。  阅读全文

posted @ 2008-09-13 00:15 飘遥(周振兴) 阅读(128) | 评论 (0)编辑

2008年9月5日

     摘要: 本篇介绍Linq的Group和Join操作。  阅读全文

posted @ 2008-09-05 23:33 飘遥(周振兴) 阅读(129) | 评论 (0)编辑

2008年9月2日

     摘要: 插件是遵循一定的编程规范实现的来扩展程序功能的组件。  阅读全文

posted @ 2008-09-02 23:11 飘遥(周振兴) 阅读(188) | 评论 (0)编辑

2008年8月26日

本篇介绍Linq的集合操作,继续使用《Linq 学习(3) 语法结构》中介绍的数据源。

Count/LongCount

Count/LongCount 返回结果集中元素的数量,返回类型分别为int/long。
原型为:
public static int Count<TSource>(this IEnumerable<TSource> source[, Func<TSource, bool> predicate])

姓名长度大于四个字符的学生的数量:
var result = (from student in DataSource.Students
              select student).Count(stu
=> { return stu.Name.Length > 4; }); // result: 1

Sum/Min/Max/Average

分别返回结果集元素或其属性的和、最小值、最大值、平均值。

Sum:返回集合元素的和,参与计算的集合元素必须为基本的值类型或可空类型。
计算总分:
var result = (from score in DataSource.Scores
              select score).Sum(score
=> { return score.Value; });

Min/Max:结果集元素的最大值或最小值,要求集合元素实现IComparable<T> 或 IComparable接口。
选择最高成绩:
var result = (from score in DataSource.Scores
              select score).Max(score
=> { return score.Value; }); // result: 96
Average:返回集合元素的平均值,参与计算的集合元素必须为基本的值类型或可空类型。
计算平均成绩:
var result = (from score in DataSource.Scores
              select score).Average(score
=> { return score.Value; }).ToString("F2"); // result: 75.23
 
Union
联合两个拥有相同或相似元素的集合,不去掉重复元素。
Union原型为:
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

var result = DataSource.Students.Union(DataSource.Students2);

// result: 两个集合的所有元素的集合。


Aggregate

Aggregate是最灵活的操作符,有些前面Sum/Min/Max/Average不支持的类型可以自己定义其实现。
Aggregate原型为:

public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)

source
要聚合的 IEnumerable<(Of <(T>)>)。
seed
累加器的初始值。
func
要对每个元素调用的累加器函数。
resultSelector
将累加器的最终值转换为结果值的函数。 

返回所有学生的姓名字符串(相当于自定义Sum):
var result = (from student in DataSource.Students
              select student.Name)
              .Aggregate(
"Name:", (a, b) => a + " " + b, c => c + " \n");
// result: Name: Andy Bill Cindy Dark

选择的学生姓名(按字符串排序取最大的,相当于实现Max):
var result = (from student in DataSource.Students
              select student.Name).Aggregate((a, b)
=> string.Compare(a, b) > 0 ? a : b);
// result: Dark

算分数的平均值(返回Score对象,相当于自定义Average):
var collection = from score in DataSource.Scores
             select score;

var result = collection.Aggregate(new Score { Value = 0 },
    (a, b)
=> { return new Score { Value = a.Value + b.Value }; },
    c
=> new Score { Value = c.Value / collection.Count() }).Value.ToString("F2");
// result: 75.23

Aggregate 可以灵活的实现各种自定义的功能。

集合操作会影响Linq的执行效率,因为执行集合操作时会遍历甚至多次遍历集合元素;对Linq的延迟执行也会变为立即执行,后面介绍性能优化时会讨论。

posted @ 2008-08-26 10:04 飘遥(周振兴) 阅读(89) | 评论 (0)编辑

2008年8月24日

这里简单介绍Linq的投影、筛选和排序子句。

Select

select 在一个集合序列按给定的条件进行投影,select 可以返回组合的筛选结果,返回匿名类型,对返回结果进行操作,返回组合的子查询结果等等。
select 的方法定义原形为:
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)

该扩展方法是在Enumerable类型中定义的。

// 数据源的类型的属性
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.Name;

// 数据源类型筛选后的结果
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student;

// 新类型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new Student { Name = student.Name, StudentID = student.StudentID };

// 匿名类型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new { Name = student.Name, StudentID = student.StudentID };

// 对返回结果进行操作
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.ToString();

由Select方法原型可看出,返回结果为:IEnumerable<T>类型。

SelectMany

SelectMany定义原型为:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)

通过原型可以看出,筛选结果的每一个元素类型都应该实现IEnumerable<T>接口。
string实现了IEnumerable<T>接口,可以构造这样的场景:查询组成学生姓名的所有字符序列。
var result = DataSource.Students.SelectMany(str => str.Name);

等价的Select 子句为:
var result = from student in DataSource.Students
             from ch in student.Name
             select ch;

可以认为SelectMany是将序列的每个元素投影到 IEnumerable<(Of <(T>)>) 并将结果序列合并为一个序列。

Distinct

原型为:
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
去掉投影结果集的重复元素。该运算只能以方法调用的方式进行操作。
上面同样的场景:查询组成学生姓名的所有字符的不重复序列。
var result = DataSource.Students.SelectMany(str => str.Name).Distinct();

First、Last、Skip、Take、Single

First 选择集和的第一个元素。
var result = DataSource.Students.Select(student => student).First(); // Student ID:1,Student Name:Andy
Last 选择集和的最后一个元素。
var result = DataSource.Students.Select(student => student).Last(); // Student ID:4,Student Name:Dark
Skip 跳过N个元素。
var result = DataSource.Students.Select(student => student).Skip(2).Count(); // 2
Take 选择集合的前N个元素。
var result = DataSource.Students.Select(student => student).Skip(2).Take(1).Count(); // 1
Single 返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
var result = DataSource.Students.Select(student => student).Single(); // 异常:Sequence contains more than one element

OrderBy[Descending]

对集合进行排序。
public static IOrderedEnumerable<TSource> OrderBy[Descending]<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector[, IComparer<TKey> comparer])

TKey必须已经实现了IComparer<T>接口。

var result = from student in DataSource.Students
             orderby student.Name descending
             select student.Name;
// 等价于:
var result = DataSource.Students.OrderByDescending(student => student.Name).Select(student => student.Name);
// 结果:
// Dark
// Cindy
// Bill
// Andy

下一篇介绍集合操作。

posted @ 2008-08-24 21:09 飘遥(周振兴) 阅读(100) | 评论 (0)编辑