假设:
public class Person { public string LastName { get; set; } } IQueryablecollection;
您的查询:
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
.编译器理解Expression<>
类型并生成代码以构建表示lambda 的表达式树:
using System.Linq.Expressions; var query = Queryable.Where( collection, Expression.Lambda>( 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);