views:

739

answers:

3

I'm using LINQPad to create LINQ queries in an application I'm bulding.

I noticed that in the downloaded LINQ in Action samples, e.g. example 4.04, intellisense shows a class "Books" but I don't see any references or "using" statements in the LINQPad tool, here is the sample:

List<Book> books = new List<Book>() {
  new Book { Title="LINQ in Action" },
  new Book { Title="LINQ for Fun" },
  new Book { Title="Extreme LINQ" } };

var titles =
  books
    .Where(book => book.Title.Contains("Action"))
    .Select(book => book.Title);

titles.Dump();

In "LinqBooks.Common, Business Objects, Book.linq" is where the class seems to be defined:

public class Book
{
  public IEnumerable<Author> Authors {get; set;}
  public String Isbn {get; set;}
  public String Notes {get; set;}
  public Int32 PageCount {get; set;}
  public Decimal Price {get; set;}
  public DateTime PublicationDate {get; set;}
  public Publisher Publisher {get; set;}
  public IEnumerable<Review> Reviews {get; set;}
  public Subject Subject {get; set;}
  public String Summary {get; set;}
  public String Title {get; set;}
  public String Test {get; set;}

  public override String ToString()
  {
    return Title;
  }
}

But how does this work so that I can copy in my classes and use LINQPad to quickly build LINQ statements that I can then copy back into my application?

+1  A: 

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

Andrew Hare
So I created a .dll with my classes in it which I want to query. I press F4 and **Add** but I don't see it in the list. How do I reference my custom .dll?
Edward Tanguay
In that same dialog you need to use the _Additional Namespace Imports_ to bring those namespaces into scope.
Andrew Hare
But all I have is a .dll which I want to access in LINQPad, e.g. "C:\temp\SmartFormTest.dll", I copy that line in *Additional Namespace Imports" but I can't reference it nor does it come up in the Add list. How can I reference **C:\temp\SmartFormTest.dll**?
Edward Tanguay
+7  A: 

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.

Winston Smith
it was the "Browse" button I was missing, thanks!
Edward Tanguay
+2  A: 

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.

David.Chu.ca
The example of xml and snip codes, my post above, are not from Book.linq. Here is just a generic example of the structure of xml and codes.
David.Chu.ca