C# 如何创建返回该类实例集合的类的构造函数?

C# 如何创建返回该类实例集合的类的构造函数?,c#,linq-to-sql,collections,constructor,C#,Linq To Sql,Collections,Constructor,我的程序具有以下类定义: public sealed class Subscriber { private subscription; public Subscriber(int id) { using (DataContext dc = new DataContext()) { this.subscription = dc._GetSubscription(id).SingleOrDefault();

我的程序具有以下类定义:

public sealed class Subscriber
{
    private subscription;
    public Subscriber(int id)
    {
        using (DataContext dc = new DataContext())
        {
           this.subscription = dc._GetSubscription(id).SingleOrDefault();                
        }            
    }
}
,在哪里

\u GetSubscription()
是一个存储过程,它返回类型为
IsingResult

比如说,我有一个
list
类型的列表,里面有1000个
id
s,我想创建一个
list
类型的订阅者集合

如果不在循环中调用构造函数1000次,我如何做到这一点

因为我试图避免频繁地打开/关闭DataContext,这可能会给数据库带来压力


TIA.

编写一个调用私有构造函数的静态工厂方法

public sealed class Subscriber
{
    // other constructors ...

    // this constructor is not visible from outside.
    private Subscriber(DataContext dc, int id)
    {
       // this line should probably be in another method for reusability.
       this.subscription = dc._GetSubscription(id).SingleOrDefault();                
    }

    public List<Subscriber> CreateSubscribers(IEnumerable<int> ids)
    {
        using (DataContext dc = new DataContext())
        {

           return ids
             .Select(x => new Subscriber(dc, x))
             // create a list to force execution of above constructor
             // while in the using block.
             .ToList();
        }            

    }

}
公共密封类订户
{
//其他构造器。。。
//此构造函数从外部不可见。
专用订户(DataContext dc,int id)
{
//这一行可能应该在另一种重用方法中。
this.subscription=dc.\u GetSubscription(id).SingleOrDefault();
}
公共列表CreateSubscribers(IEnumerable ID)
{
使用(DataContext dc=newdatacontext())
{
返回ID
.选择(x=>新订户(dc,x))
//创建一个列表以强制执行上述构造函数
//而在使用块中。
.ToList();
}            
}
}

编写一个调用私有构造函数的静态工厂方法

public sealed class Subscriber
{
    // other constructors ...

    // this constructor is not visible from outside.
    private Subscriber(DataContext dc, int id)
    {
       // this line should probably be in another method for reusability.
       this.subscription = dc._GetSubscription(id).SingleOrDefault();                
    }

    public List<Subscriber> CreateSubscribers(IEnumerable<int> ids)
    {
        using (DataContext dc = new DataContext())
        {

           return ids
             .Select(x => new Subscriber(dc, x))
             // create a list to force execution of above constructor
             // while in the using block.
             .ToList();
        }            

    }

}
公共密封类订户
{
//其他构造器。。。
//此构造函数从外部不可见。
专用订户(DataContext dc,int id)
{
//这一行可能应该在另一种重用方法中。
this.subscription=dc.\u GetSubscription(id).SingleOrDefault();
}
公共列表CreateSubscribers(IEnumerable ID)
{
使用(DataContext dc=newdatacontext())
{
返回ID
.选择(x=>新订户(dc,x))
//创建一个列表以强制执行上述构造函数
//而在使用块中。
.ToList();
}            
}
}

1000 id来自哪里?您能否更改数据库,使其使用函数而不是存储过程?谢谢您的评论。。。这不是我的选择,我只是在用一种有趣的方式进行编码,就像我被告知的那样,我的用户名在某种程度上暗示了我对他人的影响力:-)修复了代码,请看我的答案。1000 id来自哪里?您能否更改数据库,使其使用函数而不是存储过程?谢谢您的评论。。。不是我自己选择的,我只是在用一种有趣的方式来编码,就像我被告知的那样,我的用户名在某种程度上暗示了我对他人的影响力:-)修复了代码,看看我的答案。嗨,Stefan,你的答案真的很酷。但是,在释放DataContext dc时,我得到了System.ObjectDisposedException。System.ObjectDisposedException未经处理Message=“无法访问已处理的对象。\r\n对象名称:“在Dispose之后访问的DataContext”。“Source=“System.Data.Linq”ObjectName=“在Dispose之后访问的DataContext。”ThanksI尝试保存返回值的本地副本,但仍然不起作用。我通过执行类似于….的操作使其工作。。。。。返回id.select(x=>newsubscriber(dc,x)).ToList();但我不知道这里发生了什么。有一件事我非常确定,IEnumerable的生命周期与DataContext绑定在一起。是的,对不起,我修复了它。问题是lambda表达式是在访问
IEnumerable
(从调用者)时执行的。使用
IEnumerables
时出现的一个常见错误。非常感谢Stefan提供的解决方案,这真的很有帮助,我知道这就是LINQ to SQL中所谓的“延迟执行”,但仍然想知道为什么IEnumerable会随DataContext一起消失,甚至保存了一个引用。嗨Stefan,你的答案真的很酷。但是,在释放DataContext dc时,我得到了System.ObjectDisposedException。System.ObjectDisposedException未经处理Message=“无法访问已处理的对象。\r\n对象名称:“在Dispose之后访问的DataContext”。“Source=“System.Data.Linq”ObjectName=“在Dispose之后访问的DataContext。”ThanksI尝试保存返回值的本地副本,但仍然不起作用。我通过执行类似于….的操作使其工作。。。。。返回id.select(x=>newsubscriber(dc,x)).ToList();但我不知道这里发生了什么。有一件事我非常确定,IEnumerable的生命周期与DataContext绑定在一起。是的,对不起,我修复了它。问题是lambda表达式是在访问
IEnumerable
(从调用者)时执行的。使用
IEnumerables
时出现的一个常见错误。非常感谢Stefan提供的解决方案,这真的很有帮助,我知道这就是LINQ to SQL中所谓的“延迟执行”,但仍然想知道为什么IEnumerable随DataContext一起消失,甚至保存了一个引用。