LINQ Datacontext处理问题

时间:2010-04-14 18:00:44

标签: c# .net linq-to-sql datacontext

我在下面的DAL方法中得到Cannot access object: DataContext after it's been disposed。我以为我可以在那里打电话处理。 resultIEnumurable,我认为IQueryable导致了这类问题。

我做错了什么? 应该如何我将如何处置我的DataContext。有没有更好的回报然后DataTable?这是一个指向SQL 2005的桌面应用程序。

导致此错误的示例方法 - >


    public static DataTable GetEnrolledMembers(Guid workerID)
    {
        var DB = CmoDataContext.Create();

        var AllEnrollees = from enrollment in DB.tblCMOEnrollments
                           where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                           join supportWorker in DB.tblSupportWorkers on enrollment.EconomicSupportWorkerID
                               equals supportWorker.SupportWorkerID into workerGroup
                           from worker in workerGroup.DefaultIfEmpty()
                           select
                               new
                               {
                                   enrollment.ClientID,
                                   enrollment.CMONurseID,
                                   enrollment.CMOSocialWorkerID,
                                   enrollment.EnrollmentDate,
                                   enrollment.DisenrollmentDate,
                                   ESFirstName = worker.FirstName,
                                   ESLastName = worker.LastName,
                                   ESPhone = worker.Phone
                               };

        var result = from enrollee in AllEnrollees.AsEnumerable()
                     where (enrollee.DisenrollmentDate == null || enrollee.DisenrollmentDate > DateTime.Now)
                     //let memberName = BLLConnect.MemberName(enrollee.ClientID)
                     let lastName = BLLConnect.MemberLastName(enrollee.ClientID)
                     let firstName = BLLConnect.MemberFirstName(enrollee.ClientID)
                     orderby enrollee.DisenrollmentDate ascending, lastName ascending
                     select
                         new
                         {
                             enrollee.ClientID,
                             //MemberName = memberName,
                             LastName = lastName,
                             FirstName = firstName,
                             NurseName = BLLAspnetdb.NurseName(enrollee.CMONurseID),
                             SocialWorkerName = BLLAspnetdb.SocialWorkerName(enrollee.CMOSocialWorkerID),
                             enrollee.EnrollmentDate,
                             enrollee.DisenrollmentDate,
                             ESWorkerName = enrollee.ESFirstName + " " + enrollee.ESLastName,
                             enrollee.ESPhone
                         };
        DB.Dispose();

        return result.CopyLinqToDataTable();
    }

我创建DataContext的部分类 - >


partial class CmoDataContext
{
    public static bool IsDisconnectedUser
    {
        get
        {
            return Settings.Default.IsDisconnectedUser;
        }
    }

    public static CmoDataContext Create()
    {
        var cs = IsDisconnectedUser ? Settings.Default.CMOConnectionString : Settings.Default.Central_CMOConnectionString;
        return       new CmoDataContext(cs);
    }

3 个答案:

答案 0 :(得分:7)

在处理DataContext后,您正在调用CopyLinqToDataTable。由于LINQ使用延迟执行,因此只有在放置DataContext后枚举result时才执行查询。

您应该将DataContext放在包含using语句的return块中 这将在调用CopyLinqToDataTable后处理DataContext,避免出现问题。

例如:

using(var DB = CmoDataContext.Create()) {
    //Execute queries
    return result.CopyLinqToDataTable();
}

using语句生成一个finally块,在最后处理DataContext using块的。{ (即使抛出异常)

答案 1 :(得分:4)

在调用ToList

之前,您应该在查询中使用Dispose之类的内容

答案 2 :(得分:1)

你不能这样做吗?

...
DataTable dt = result.CopyLinqToDataTable(); 
DB.Dispose(); 
return dt;