我有两个派生自BankAccount类的类 - FixedBankAccount和SavingsBankAccount。这是基于LINQ to SQL的“TPH(Table Per Hierarchy)”。
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.BankAccount")]
[InheritanceMapping(Code = "Fixed", Type = typeof(FixedBankAccount), IsDefault = true)]
[InheritanceMapping(Code = "Savings", Type = typeof(SavingsBankAccount))]
public abstract partial class BankAccount : INotifyPropertyChanging, INotifyPropertyChanged
我需要实现一个方法GetAllAccountsOfType,它将返回所有SavingsBankAccount(如果传递的类型是SavingsBankAccount)或FixedBankAccounts(如果传递的类型是FixedBankAccount)。我收到了错误:
无法找到类型或命名空间名称'param'“
使用以下代码(在LINQ查询中使用“OfType”)
我们怎样才能让它发挥作用?
存储库:
namespace RepositoryLayer
{
public class LijosSimpleBankRepository : ILijosBankRepository
{
public System.Data.Linq.DataContext Context { get; set; }
public virtual List<DBML_Project.BankAccount> GetAllAccountsOfType(DBML_Project.BankAccount param)
{
var query = from p in Context.GetTable<DBML_Project.BankAccount>().OfType<param>()
select p;
}
}
}
答案 0 :(得分:5)
声明:
public IEnumerable<T> GetAllAccounts<T>()
where T : BankAccount // T must inherit class BankAccount, like your two sub-classes do
{
return Context.GetTable<DBML_Project.BankAccount>().OfType<T>();
}
用法:
var allSaving = GetAllAccounts<SavingsBankAccount>();
var allFixed = GetAllAccounts<FixedBankAccount>();