Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:06:21+00:00 2026-05-31T08:06:21+00:00

Im writing DLL for one project, I just started using LINQ to SQL and

  • 0

Im writing DLL for one project, I just started using LINQ to SQL and after moving all methods to this new dll. I disovered that I can’t acess DataContext because it was disposed, I understand why but I’m not sure how I can acess results of query for my main project method so:

My method in DLL

 public static IEnumerable<Problem> GetProblemsNewest(int howMuch)
        {
            using (ProblemClassesDataContext context = new ProblemClassesDataContext())
            {
                var problems = (from p in context.Problems orderby p.DateTimeCreated select p).Take(howMuch);

                return problems;
            }
        }

Calling it:

IEnumerable<Problem> problems = ProblemsManipulation.GetProblemsNewest(10);
//Error can't acess it because it was disposed..

This is just first method, I have larger ones so I really need a way to do this. There must be a way to use LINQ to SQL in DLL? I know I can do something Like .ToList or .ToArray but then I wouldn’t be able to acess row properties directly and would have to reference it as problem[0],problem[1] etc. which is even more messy than having tone of code in main project.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:06:23+00:00Added an answer on May 31, 2026 at 8:06 am

    After you are outside of the using statement the context is automatically disposed, so when the IEnumerable is actually enumerated the context is already disposed.

    Therefore you need to tell Linq that it should go ahead and actually retrieve the values from the DB while your inside of your using statement. You can do so via ToList() or ToArray (or others).

    See the updated code:

    public static IList<Problem> GetProblemsNewest(int howMuch)
    {
        using (ProblemClassesDataContext context = new ProblemClassesDataContext())
        {
            var problems = (from p in context.Problems orderby p.DateTimeCreated select p).Take(howMuch);
    
            return problems.ToList();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me with this one, I've been writing a console applicaiton using the
I am writing a Win32 DLL library that can be redistributed. I am using
I'm writing some custom .Net extensions for SQL Server 2008. One of them is
This is all hypothetical, so please bear with me. Say I'm writing a tool
I'm writing a DLL for an audio-player (foobar2000) using its SDK. My DLL links
My project has several new C# modules and one C module (not C++) compiled
I'm writing a C++ DLL that loads another DLL during one of its functions.
I have been writing DLL on C++, that will be use in C#. DLL
I am writing a DLL with mixed C/C++ code. I want to specify the
I'm writing a DLL to change permissions on a folder and everything underneath the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.