How LINQPad refers to other classes, for example. Books in LINQ in Action Samples

I am using LINQPad to create LINQ queries in an application that I am expanding.

I noticed that in loaded LINQ in Action samples, for example. example 4.04, intellisense shows the "Books" class, but I don’t see any links or using the "in the LINQPad tool, here is an example:

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," Book.linq business objects "is a class that appears 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 it work so that I can copy in my classes and use LINQPad to quickly create LINQ statements that can then be copied back to my application?

+58
c # linq linq-to-objects linqpad
Aug 03 '09 at 12:15
source share
4 answers

If you right-click in the LINQPad code editor and select "Advanced query properties", two dialogs will appear: "Additional links" and "Import additional names".

1) In the Additional links, select Add , then click Browse and go to your custom assembly.

2) Then, in the Import additional namespace, enter the namespaces that you want to import from this assembly.

+98
Aug 03 '09 at 12:36
source share

LINQPad allows you to reference custom assemblies in the Advanced Queries dialog box, which can be opened by pressing F4 .

+9
Aug 03 '09 at 12:24
source share

Actually, if you look at the linq file, such as Book.linq with notepad, you will see that the file is a mixture of XML and a piece of code 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 words, you can find more detailed information from examples of linq files about how LINQPad receives all the information, creates a dynamic assembly and runs it inside to return the results to its interface.

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

+4
Sep 18 '09 at 14:26
source share

Edward, we used a number of strategies when building LINQ in Action patterns. In database chapters, we often simply relied on LINQPad's ability to automatically generate classes based on database tables.

In the link given here (4.04), we added a link to a precompiled class library using F4. We used this strategy in cases where LinqPad generated classes other than those created by Visual Studio, and thus led to the fact that the context behaves differently than you expected, especially in relation to change tracking.

In other cases, we added a nested nested class along with the rest of the selection and used the "Program" option in the code editor. See Example 6.02. In this case, we actually implement the Books class inside the created DataContext class, which generates LinqPad. We also used this strategy when we wanted to get aliases for our column names, because the automatically generated classes created by LinqPad do not easily allow us to use these columns inside the tool.

In a few examples, especially in cases where we demonstrate our own extension methods, we had to do one more trick for forcing the completion of the created context class (adding an explicitly inconsistent ending) or End Class), and then launching a new class, but omitting its trailing end / end of class. This can be seen in Example 2.16 in the loaded samples.

+2
Jun 13 2018-11-11T00:
source share



All Articles