LINQPad 如何引用其他类,例如 LINQ in Action 示例中的书籍

发布于 2024-07-30 04:20:35 字数 1275 浏览 10 评论 0原文

我正在使用 LINQPad 在我正在构建的应用程序中创建 LINQ 查询。

我注意到在下载的LINQ in Action示例中,例如示例4.04,intellisense显示了一个类“Books”,但我没有看到任何引用或“使用” LINQPad 工具中的语句,以下是示例:

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();

在“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;
  }
}

但是如何这样我就可以复制我的类并使用 LINQPad 快速构建 LINQ 语句,然后将其复制回我的应用程序吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乜一 2024-08-06 04:20:35

Edward,我们在构建 LINQ in Action 示例时使用了多种策略。 在数据库章节中,我们通常只依赖 LINQPad 根据数据库表自动生成类的能力。

对于您在此处引用的情况 (4.04),我们确实使用 F4 添加了对预编译类库的引用。 我们在 LinqPad 生成的类与 Visual Studio 生成的类不同并因此导致上下文的行为与您预期不同的情况下使用了此策略,特别是在更改跟踪方面。

在其他情况下,我们添加了一个与示例其余部分内联的嵌套类,并在代码编辑器中使用“程序”选项。 参见示例 6.02。 在本例中,我们实际上将 Books 类嵌入到 LinqPad 生成的 DataContext 类中。 当我们想要为列名添加别名时,我们也使用了此策略,因为 LinqPad 创建的自动生成的类不允许我们在工具内为这些列添加别名。

在几个示例中,特别是在我们演示自定义扩展方法的情况下,我们必须执行另一个技巧,强制生成的上下文类完成(添加明显不匹配的结尾 } 或 End Class),然后启动一个新类,但忽略它的结束结束大括号/结束类。 您可以在下载的示例中的示例 2.16 中看到这一点。

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.

身边 2024-08-06 04:20:35

如果右键单击 LINQPad 中的代码编辑器并选择“高级查询属性”,则会出现两个对话框:“附加引用”和“附加命名空间导入”。

1) 在其他引用中,选择添加,然后单击浏览并导航到您的自定义程序集。

2) 然后,在其他命名空间导入中,键入要从该程序集中导入的命名空间

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.

自此以后,行同陌路 2024-08-06 04:20:35

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

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

相思故 2024-08-06 04:20:35

实际上,如果你用记事本查看 linq 文件,例如 Book.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=...";
....

换句话说,你可以从示例 linq 文件中找到更多详细信息关于 LINQPad 如何获取所有信息、构建动态程序集并在内部运行它以将结果返回到其 UI。

顺便说一句,我昨晚写了一篇博客,介绍了这个工具以及我对其结构的理解:LINQPad .Net 片段代码 IDE

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>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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文