简体   繁体   中英

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

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

I noticed that in the downloaded LINQ in Action samples, eg 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?

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.

LINQPad允许您通过“ 高级查询属性”对话框引用自定义程序集,可以通过按F4打开该对话框。

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM