How to Get Linq to Prepopulate/Explicitly Fetch Data From the Database

Several people have commented to me on using LINQ to SQL and getting an ObjectDisposedException message when trying to access the returned data.

Exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'DataContext accessed after Dispose.'

This can occur when using Linq to SQL to retrieve data from a database – and when you try to access [...]

Read Users' Comments (0)

How to Retrieve Multiple Recordsets Using LINQ to SQL

LINQ to SQL provides a lot of features to make it easier and faster for us to connect to a database and call stored procedures. Unfortunately, there is still a lack of built-in support for retrieving multiple recordsets. I am going to show you a way to force LINQ to SQL to return multiple recordsets.

Caveat: [...]

Read Users' Comments (0)

Parsing Parent Nodes of an XML File Usinq LINQ to XML

Recently, a reader submitted an interesting problem to me. He had an XML file which he was trying to parse into a class, but he was having difficulty populating the class with both the selected results and their parent's values.

After a little experimentation, I realized you can call the Parent property of an XDocument queried [...]

Read Users' Comments (0)

Cache the Result of a LINQ to SQL DataContext

I recently read a blog from a developer complaining that you cannot cache the result of a LINQ to SQL DataContext. I want to set the record straight and demonstrate how you can cache the result of  LINQ to SQL DataContext.

Before we continue, I would like to point out something. In the original post, the [...]

Read Users' Comments (0)

Attaching a TextWriter to a LINQ DataContext

Sometimes we need to log the commands and results of querying the database using LINQ. This could be for performance tuning reasons, general logging practices, or some other reason. Microsoft has made it easy for us to attach a TextWriter object to the LINQ DataContext, which will log all commands and results issued on the [...]

Read Users' Comments (0)

Performance Issues When Using Let in LINQ

Let is a powerful keyword in LINQ which allows us to assign a value to a temporary variable which can be reused in the LINQ statement. However, there can be performance penalties for using Let incorrectly.

Examples of Using Let in LINQ

static void TestBaseline()
{
    var q = from c in Customer.AllCustomers
        select c;
    int count [...]

Read Users' Comments (0)

How to Perform Multiple Actions on Items in a List Using LINQ Select

Sometimes we'll have a list of data which needs to have multiple actions performed on each item in the list. An example might be to write the item's value to a log, call a function using the item as a param to the function, and then convert the item to a new type. In the [...]

Read Users' Comments (0)

How to Use LINQ to Filter a List of Files by Date

Sometimes we have to write applications which need to retrieve a list of files filtered by the date the file was created or last written to. A common example is for applications which need to delete a list of files older than a certain date. For example, I have an application which creates a new [...]

Read Users' Comments (0)