How does LINQPad reference other classes, e.g. Books in the LINQ in Action samples

Solution 1:

If you right click in the code editor in LINQPad and choose Advanced Query Properties, there are two dialogs: Additional References and Additional Namespace Imports.

1) In Additional References, choose Add then click Browse and navigate to your custom assembly.

2) Then, in Additional Namespace Imports, type the namespaces you want to import from that assembly.

Solution 2:

LINQPad allows you to reference custom assemblies through the Advanced Query Properties dialog which can be opened by pressing F4.

Solution 3:

Actually, if you look at the linq file such as Book.linq with notepad, you will see the file is a mixture of XML and a snippet of codes at the end:

<Query Kind="Statements"> <!-- kind: Program, ... --->
  <Connection>...</Connection> <!-- Optional, if you have connection to db -->
  <Reference>[path]\[library]</Reference> <!-- references to your customized libraries -->
  <Reference>RuntimeDirectory&gt;System.Data.dll</Reference> <!-- example to System.Data.dll -->
  <Namespace>System.Data</Namespace> <!-- here are nodes for namespaces... -->
  <Namespace>MyLibrary.Common</Namespace>
</Query>

var conn = "Data Source=...";
....

In order words, you may find more detail information from example linq files about how LINQPad gets all the information out, builds a dynamic assembly and runs it internally to get results back to its UI.

By the way, I wrote a blog last night about this tool and my understanding of its structure: LINQPad a .Net Snippet Code IDE.

Solution 4:

Edward, we used a number of strategies when building the LINQ in Action samples. In the database chapters, we often just relied on LINQPad's ability to autogenerate the classes based on the database tables.

In the case you reference here (4.04) we did add the reference to the pre-compiled class library using F4. We used this strategy in cases where LinqPad generated classes different from those generated by Visual Studio and thus caused the context to behave differently than you would expect, particularly in regards to change tracking.

In other cases, we added a nested class inline with the rest of the sample and used the "Program" option in the code editor. See example 6.02. In this case, we're actually imbedding the Books class inside of the generated DataContext class that LinqPad generates. We also used this strategy when we wanted to alias our column names because the auto-generated classes that LinqPad creates doesn't easily let us alias those columns inside the tool.

In a couple samples, particularly where we are demonstrating custom extension methods, we had to do another trick of forcing the generated context class to finish (adding an aparently unmatched ending } or End Class) and then starting a new class, but omitting it's closing end brace/end class. You can see this in example 2.16 in the downloaded samples.