我有兩個派生自BankAccount類的類 - FixedBankAccount和SavingsBankAccount。這是基於「TPH(Table Per Hierarchy)」和LINQ to SQL的工作。在LINQ中使用「OfType」
[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)。我得到的錯誤:
The type or namespace name 'param' could not be found」
用下面的代碼(它使用的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;
}
}
}
參考:http://stackoverflow.com/questions/14603815/how-to-write-低於邏輯的通用方式 – Lijo