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

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

Posted by: admin November 29, 2017 Leave a comment

Questions:

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?

Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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.