C# LINQPad 如何引用其他类,例如 LINQ in Action 示例中的 Books
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does LINQPad reference other classes, e.g. Books in the LINQ in Action samples
提问by Edward Tanguay
I'm using LINQPad to create LINQ queries in an application I'm bulding.
我正在使用 LINQPad 在我正在构建的应用程序中创建 LINQ 查询。
I noticed that in the downloaded LINQ in Actionsamples, e.g. example 4.04, intellisense shows a class "Books" but I don't see any referencesor "using" statements in the LINQPad tool, here is the sample:
我注意到在下载的LINQ in Action示例中,例如示例 4.04,intellisense 显示了一个类“Books”,但我在 LINQPad 工具中没有看到任何引用或“ using”语句,这是示例:
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:
在 "LinqBooks.Common, Business Objects, Book.linq" 中似乎定义了类:
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?
但是这是如何工作的,以便我可以在我的类中复制并使用 LINQPad 快速构建 LINQ 语句,然后我可以将其复制回我的应用程序?
采纳答案by Winston Smith
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.
如果在 LINQPad 中的代码编辑器中右键单击并选择 Advanced Query Properties,则会出现两个对话框:Additional References 和 Additional Namespace Imports。
1) In Additional References, choose Addthen click Browseand navigate to your custom assembly.
1) 在Additional References 中,选择Add然后单击Browse并导航到您的自定义程序集。
2) Then, in Additional Namespace Imports, type the namespacesyou want to import from that assembly.
2) 然后,在Additional Namespace Imports 中,键入要从该程序集中导入的命名空间。
回答by Andrew Hare
LINQPad allows you to reference custom assemblies through the Advanced Query Propertiesdialog which can be opened by pressing F4.
LINQPad 允许您通过“高级查询属性”对话框引用自定义程序集,该对话框可以通过按 打开F4。
回答by David.Chu.ca
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:
实际上,如果您使用记事本查看诸如 Book.linq 之类的 linq 文件,您会看到该文件是 XML 和最后一段代码的混合:
<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>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.
换句话说,您可以从示例 linq 文件中找到有关 LINQPad 如何获取所有信息、构建动态程序集并在内部运行以将结果返回到其 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.
顺便说一句,我昨晚写了一篇关于这个工具和我对它结构的理解的博客:LINQPad a .Net Snippet Code IDE。
回答by Jim Wooley
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.
Edward,我们在构建 LINQ in Action 示例时使用了多种策略。在数据库章节中,我们经常仅仅依赖于 LINQPad 基于数据库表自动生成类的能力。
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.
在您引用此处 (4.04) 的情况下,我们确实使用 F4 添加了对预编译类库的引用。如果 LinqPad 生成的类与 Visual Studio 生成的类不同,从而导致上下文的行为与您预期的不同,特别是在更改跟踪方面,我们会使用此策略。
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.
在其他情况下,我们添加了一个与示例其余部分内联的嵌套类,并在代码编辑器中使用了“程序”选项。见例 6.02。在这种情况下,我们实际上是将 Books 类嵌入到 LinqPad 生成的 DataContext 类中。当我们想为列名设置别名时,我们也使用了这种策略,因为 LinqPad 创建的自动生成的类不容易让我们在工具内为这些列设置别名。
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.
在几个示例中,特别是在我们演示自定义扩展方法的地方,我们必须执行另一个技巧来强制生成的上下文类完成(添加一个明显不匹配的结尾 } 或 End Class),然后开始一个新类,但省略它是关闭结束大括号/结束类。您可以在下载的示例中的示例 2.16 中看到这一点。