Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在LINQ中使用“of type”_C#_.net_Linq_Linq To Sql_Generics - Fatal编程技术网

C# 在LINQ中使用“of type”

C# 在LINQ中使用“of type”,c#,.net,linq,linq-to-sql,generics,C#,.net,Linq,Linq To Sql,Generics,我有两个从BankAccount类派生的类–FixedBankAccount和SavingsBankAccount。这是基于“每个层次结构的TPH表”和LINQ to SQL进行的 [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.BankAccount")] [InheritanceMapping(Code = "Fixed", Type = typeof(FixedBankAccount), IsDefault = true

我有两个从BankAccount类派生的类–FixedBankAccount和SavingsBankAccount。这是基于“每个层次结构的TPH表”和LINQ to SQL进行的

[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
我需要实现一个getAllAccountSoftType方法,如果传递的类型是SavingsBankAccount,则该方法将返回所有SavingsBankAccount;如果传递的类型是FixedBankAccount,则返回FixedBankAccounts。我得到一个错误:

找不到类型或命名空间名称“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;
        }
    }
}
声明:

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>();
声明:

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>();

谢谢。现在我明白了泛型的概念。当所有类型的逻辑都相同时,使用泛型..我参考了关于geenerics的更多理解。但它并没有告诉我们在什么地方可以使用通用方法。有没有什么好文章强调,当所有类型的逻辑都相同时,使用泛型方法?谢谢。现在我了解了泛型的概念。当所有类型的逻辑都相同时,使用泛型..我参考了关于geenerics的更多理解。但它并没有告诉我们在什么地方可以使用通用方法。有没有好文章强调,当所有类型的逻辑都相同时,比如使用泛型方法?参考:参考: