dynamic
c# 5.0 (.Net 4.5) でキーワードを使用して動的 LINQ クエリを作成することはできますか?
サードパーティのライブラリを使用してこれが可能であることは知っていますが、現時点では実行できません。
私の要点を説明する最も簡単な方法は、例を使用することです。
class test
{
public int i { get; set; }
}
void Foo()
{
var collection = new[] { new test() { i = 1 }, new test() { i = 2 } };
Bar(collection);
}
void Bar<T>(IEnumerable<T> collection)
{
//this works
foreach (dynamic item in collection)
if (item.i == 2)
{
//do something
}
//this does not - although this is what id like to use
foreach (dynamic item in collection.Where(a => a.i == 2))
{
//do something
}
}
リクエストごとに編集: コンパイラ エラーが発生します -
'T' には 'i' の定義が含まれておらず、タイプ 'T' の最初の引数を受け入れる拡張メソッド 'i' が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)