何时使用 LINQ2SQL 加载相关表中的记录

发布于 2024-07-09 11:27:23 字数 257 浏览 8 评论 0原文

假设我有两个表:

  • 报告
  • 评论

假设我有一个数据库上下文:

var reports = db.Reports();

如何确保每个报告的所有评论都已加载?

此时我想断开与数据库的连接但仍然 可以访问评论。 (例如:)

reports[0].Comments[0].Subject

Let's say I have two tables:

  • Report
  • Comment

And assuming I have a database context:

var reports = db.Reports();

How can I make sure all Comments for each report are loaded as well?

At this point I want to disconnect from the database but still
have access to the comments. (For example:)

reports[0].Comments[0].Subject

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

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

发布评论

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

评论(2

遮了一弯 2024-07-16 11:27:23

我假设报告和评论之间存在 1-M FK 关系(1 个报告可以有许多评论)?

一种选择是使用 DataLoadOptions.LoadWith< /a> 方法 - 类似于以下内容:

var reports = db.Reports();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Reports>(r => r.Comments);      // Ask for Comments along with reports
reports.LoadOptions = dlo;

现在,每次您选择该数据上下文的报告时,都会从数据库中获取注释。

请注意,注释中的所有字段都将被选择 - 无法使用此方法来选择字段的子集。

另一种选择是具体说明您要在 Linq 查询中选择的内容,例如

var myReportsList = from report in db.Reports
                    select new {  // Using anonymous type, but could use a custom class
                       Report = report,
                       Comment = report.Comment.Detail,   // for example
                       Subject = report.Comment.Subject
                    };

要了解查询何时运行以及数据库连接何时关闭,您需要了解:

  • Linq 和 Linq To Sql 的延迟执行模型(基本上,对于 Linq to SQL,查询仅在需要结果时运行,例如通过迭代集合或绑定到网格)
  • IQueryable 和 IEnumerable 之间的差异

Jon Skeets“C# 深入”给出了这些的一个很好的概述,并且我'我们还听说过有关“Linq in Action”的好消息 - 另外还有很多关于这些概念的博客文章,这些文章比我在这里能做到的更公正地阐述主题;o)

I'm assuming that there is an 1-M FK relationship between reports and comments (1 Report can have many Comments)?

One option is to use the DataLoadOptions.LoadWith method - something like the following:

var reports = db.Reports();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Reports>(r => r.Comments);      // Ask for Comments along with reports
reports.LoadOptions = dlo;

Now, every time you select a report on that data context, the comments will be fetched from the db along with it.

Just beware that ALL fields from comments will be selected - there is no way using this method to select a subset of fields.

Another option is to be specific about what you want to select in the Linq query, e.g.

var myReportsList = from report in db.Reports
                    select new {  // Using anonymous type, but could use a custom class
                       Report = report,
                       Comment = report.Comment.Detail,   // for example
                       Subject = report.Comment.Subject
                    };

To understand when the query gets run and the database connection closed, you will need to understand:

  • The deferred execution model of Linq and Linq To Sql (Basically, for Linq to SQL, the query only runs when the results are asked for e.g. by iterating over the collection or binding to a grid)
  • The difference between IQueryable and IEnumerable

Jon Skeets "C# in depth" gives a great overview of these, and i've also heard very good things about "Linq in Action" - plus there are plenty of blog posts about these concepts which do the subjects more justice than I can do here ;o)

屋檐 2024-07-16 11:27:23

请记住,如果您使用 LoadOptions 定义多跳路径(报告、注释、另一个实体),则通过代码加载第三跳和更多跳(如果与 1:n 关系相关),效率非常低:它们将执行每个家长一次查询。 对于报告评论,没关系,它们将在 2 个查询中获取。

Keep in mind that if you use LoadOptions to define a multi-hop path (Reports, comments, anotherentity), the 3rd and further hops are loaded (if related over 1:n relationships) by code which is very inefficient: they'll execute one query per parent. For reports-comments, it's ok, they'll be fetched in 2 queries.

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