C# 3.0新特性之LambdaExpressions(Lambda表达式)、QuerySyntax(查询语法)和AnonymousTypes(匿名类型)
时间:2011-05-05 来源:※森林小居※
介绍
·LambdaExpressions(Lambda表达式)
·QuerySyntax(查询语法)
·AnonymousTypes(匿名类型)
示例
LambdaExpressions.cs(Lambda表达式)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
/**//// <summary>
/// LambdaExpressions(Lambda表达式)的摘要说明
/// </summary>
public class LambdaExpressions
{
public int ID { get; set; }
public string Name { get; set; }
public void LambdaExpressionsTest()
{
List<LambdaExpressions> list = new List<LambdaExpressions>
{
new LambdaExpressions { ID = 1, Name = "webabcd" },
new LambdaExpressions { ID = 2, Name = "webabcdefg" },
new LambdaExpressions { ID = 3, Name = "webabcdefghijklmn" }
};
IEnumerable<LambdaExpressions> l = list.Where(le => le.Name == "webabcd");
// 上面的(Lambda表达式)等同于下面的(匿名方法)
// IEnumerable<LambdaExpressions> l2 = list.Where(delegate(LambdaExpressions le) { return le.Name == "webabcd"; });
// 相关委托
// public delegate TResult Func<T, TResult>(T arg);
// 相关Where扩展方法
// Func<TSource, bool>:接受一个类型为TSource的参数
// Func<TSource, bool>:某个需要满足的条件,返回bool值
// public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
// {
// foreach (TSource item in source)
// {
// if (predicate(item))
// {
// yield return item;
// }
// }
// }
}
}
QuerySyntax.cs(查询语法)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
/**//// <summary>
/// QuerySyntax(查询语法)的摘要说明
/// </summary>
public class QuerySyntax
{
public int ID { get; set; }
public string Name { get; set; }
public void QuerySyntaxTest()
{
List<QuerySyntax> list = new List<QuerySyntax>
{
new QuerySyntax { ID = 1, Name = "webabcd" },
new QuerySyntax { ID = 2, Name = "webabcde" },
new QuerySyntax { ID = 2, Name = "webabcdef" },
new QuerySyntax { ID = 2, Name = "webabcdefg" },
new QuerySyntax { ID = 2, Name = "webabcdefgh" },
new QuerySyntax { ID = 2, Name = "webabcdefghi" },
new QuerySyntax { ID = 2, Name = "webabcdefghij" },
new QuerySyntax { ID = 2, Name = "webabcdefghijk" },
new QuerySyntax { ID = 2, Name = "webabcdefghijkl" },
new QuerySyntax { ID = 2, Name = "webabcdefghijklm" },
new QuerySyntax { ID = 3, Name = "webabcdefghijklmn" }
};
IEnumerable<QuerySyntax> l = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select o;
// 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式)
// 查询语法相对更容易理解
// IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length);
// Projection(映射)
// 可以返回一个新的类型
IEnumerable<Projection> l2 = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select new Projection { Name = o.Name };
}
}
/**//// <summary>
/// 为了演示Projection(映射)而写的实体类
/// </summary>
public class Projection
{
public string Name { get; set; }
}
AnonymousTypes.cs(匿名类型)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
/**//// <summary>
/// AnonymousTypes(匿名类型)的摘要说明
/// </summary>
public class AnonymousTypes
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public void AnonymousTypesTest()
{
List<AnonymousTypes> list = new List<AnonymousTypes>
{
new AnonymousTypes { ID = 1, Name = "webabcd", Age = 10 },
new AnonymousTypes { ID = 2, Name = "webabcdefg", Age = 20 },
new AnonymousTypes { ID = 3, Name = "webabcdefghijklmn", Age = 30 }
};
// listAnonymousTypes - 匿名类型
var listAnonymousTypes = from l in list
where l.Name == "webabcd"
select new { Name = l.Name, Age = l.Age };
foreach (var v in listAnonymousTypes)
{
// v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
string name = v.Name;
int age = v.Age;
}
// 声明匿名类型:将new关键词后面的类型名称省略掉
var person = new { Name = "webabcd", Age = 27 };
// person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
string myName = person.Name;
int myAge = person.Age;
}
}
[源码下载]