私はこのエンティティを持っています:
public class Parent: AllDependant
{
/*Properties goes here*/
}
public class Children: AllDependant
{
/*Properties goes here*/
}
次にallDependants
、typeofを使用して変数をList<AllDependant>
作成します。これにより、親と子のエンティティの一部が混在し続けます。
後で、それらから選択して、次のようなことをしたいと思います。
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基本クラスに特別なプロパティを配置できません。これは面倒です。ただし、上記のLinqSelect
拡張メソッドを使用して実行しましたが、私はこれについて考えています。
質問:Linqクエリで同じことを行うにはどうすればよいですか?
select
これにより、キーワードと中括弧にエラーが発生します。
var selectedDependantInfos = from dependant in allDependants
select
{
/* the same if statement goes here */
}