假设:
public class Person
{
public string LastName { get; set; }
}
IQueryable<Person> collection;
查询:
var query =
from p in collection
where p.LastName == textBox.Text
select p;
装置相同:
var query = collection.Where(p => p.LastName == textBox.Text);
,编译器从扩展方法转换为:
var query = Queryable.Where(collection, p => p.LastName == textBox.Text);
的Queryable.Where
第二个参数是一个Expression<Func<Person, bool>>
。编译器理解Expression<>
类型,并生成代码来构建表示拉姆达一个expression tree:
using System.Linq.Expressions;
var query = Queryable.Where(
collection,
Expression.Lambda<Func<Person, bool>>(
Expression.Equal(
Expression.MakeMemberAccess(
Expression.Parameter(typeof(Person), "p"),
typeof(Person).GetProperty("LastName")),
Expression.MakeMemberAccess(
Expression.Constant(textBox),
typeof(TextBox).GetProperty("Text"))),
Expression.Parameter(typeof(Person), "p"));
即查询语法的意思。
您可以自由调用这些方法。
typeof(Person).GetProperty("LastName")
有:要更改属性相比,取代这个
typeof(Person).GetProperty(dropDown.SelectedValue);
这些文章是完美的,如果你使用ASP.NET应用程序,好知道,感谢您的工作,遗憾的是使用Silverlight,系统。 Windows.Threading不支持动态LINQ库中使用的一些方法 – 2009-02-02 16:46:20
啊... Silverlight标签在我的盲点。 – Kev 2009-02-02 17:32:25