1

BankAccount クラスから派生した 2 つのクラス、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

すべての SavingsBankAccount (渡されたタイプが SavingsBankAccount の場合) または FixedBankAccounts (渡されたタイプが FixedBankAccount の場合) を返すメソッド GetAllAccountsOfType を実装する必要があります。エラーが発生しています:

タイプまたはネームスペース名「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;
        }
    }
}
4

1 に答える 1

6

宣言:

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>();
于 2012-07-02T12:33:39.250 に答える