ActionBase,ActionA,ActionB和ActionC是實體(來自數據庫)。 ActionA,ActionB和ActionC是派生類型的ActionBase。通過接口轉換訪問屬性
ActionB和ActionC使用SpecialProperty實現ISpecialAction。
例如:
public interface ISpecialAction
{
Guid SpecialProperty { get; }
}
public partial class ActionBase
{
public objectX OnePropertyBase { get; set; }
}
public partial class ActionA : ActionBase
{
public objectY OnePropertyA { get; set; }
}
public partial class ActionB:ActionBase,ISpecialAction
{
public objectZ OnePropertyB { get; set; }
public Guid SpecialProperty
{
get
{
return OnePropertyB.ID;
}
}
}
public partial class ActionC : ActionBase ,ISpecialAction
{
public objectW OnePropertyC { get; set; }
public Guid SpecialProperty
{
get
{
return OnePropertyC.ID;
}
}
}
我的問題是SpecialProperty從對象(ActionB或ActionC),當演員(以ISpecialAction)完成,OtherProperty和OtherProperty2是空的其他屬性建立。 我想:
GetActionBase().ToList().Where(x=>x is ISpecialAction && ((dynamic) x).SpecialProperty== p_SpecialProperty);
GetActionBase().ToList().Where(x=>x is ISpecialAction && ((ISpecialAction) x).SpecialProperty== p_SpecialProperty);
GetActionBase().ToList().OfType<ISpecialAction>().Where(x => x.SpecialProperty== p_SpecialProperty).Cast<ActionBase>();
return GetActionOnGoing().ToList().OfType<ICityAction>().Cast<ActionBase>().Where(x => ((dynamic)x).CityId == p_CityId);
備註:OfType<>
不與LINQ到實體的接口工作,但是確定在LINQ to對象
如何訪問我的財產接口,無需知道對象的類型?
我假定你的意思'OnePropertyA'和'OnePropertyB'在'OtherProperty'和'OtherProperty2'代替?那'OnePropertyA'和'OnePropertyB'是映射的屬性? –