仮定:
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);
の2番目のパラメータはQueryable.Where
ですExpression<Func<Person, bool>>
。コンパイラは型を理解し、ラムダを表す式ツリーExpression<>
を構築するためのコードを生成します。
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);