假設:
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