私は、このエンティティを持っている:LINQクエリ
public class Parent: AllDependant
{
/*Properties goes here*/
}
public class Children: AllDependant
{
/*Properties goes here*/
}
それから私は、ミックスの中で両親と子供entitiyの一部を維持しますtypeofをList<AllDependant>
とallDependants
変数を持っています。
その後、私は彼らから選択して、このような何かやりたい:
var selectedDependantInfos = allDependants
.Select(dependant =>
{
if (dependant is Parent)
{
var parent = dependant as Parent;
return new { Name = parent.Name, SomeSpecialInfo = parent.ParentInfo };
}
else
{
var child = dependant as Children;
return new { Name = child.Name, SomeSpecialInfo = child.ChildInfo }
}
});
注子と親のための特定のプロパティは、キャストやUIの表示のための新しいモデルにプロパティを取得するために私を必要としていますエンティティの関心事ではありません。 * .ascxを含む非常に多くのファイルでプロパティ名をリファクタリングする必要があるため、AllDependantベースクラスに特別なプロパティを入れることができません。これは面倒です。しかし、上記のLinq Select
拡張メソッドを使用して完了しましたが、私はちょうどこれを考えています:
質問:Linqクエリで同じことをどうやって行えますか?あなたは条件演算子を使用して
from dependant in allDependants
select dependant is Parent
? new { Name = (dependant as Parent).Name, /* Parent fields */ }
: new { Name = (dependant as Children).Name, /* Child fields */ }
のようなものになるだろう
var selectedDependantInfos = from dependant in allDependants
select
{
/* the same if statement goes here */
}
を使用する別の方法は何あなたはLINQの構文にそれを変更することで獲得すると思いますか? – ChrisWue
エラーは 'new'キーワードがないためです。 '新しい{..}を選択します。 – alexn
@ChrisWue、ちょうど私たちがLambdaで関数体を行うことができるように、Linqクエリで関数本体を行う方法を考え直すことはありません。 – CallMeLaNN