使用 LINQ 进行双重嵌套 OData 收集

发布于 2024-10-07 17:32:39 字数 497 浏览 5 评论 0原文

我有一个用于书籍的自定义 OData 提要。每本书可以有多个作者,并且一个作者可以参与多本书,因此我使用连接表(Book - BookAuthorJoin - Author)来实现这一点。我的代理对象有 Book.BookAuthorJoins BookAuthorJoin.Books & BookAuthorJoin.Authors。

我想要做的是有一个查询,我可以在单个 LINQ 查询中获取某个作者的所有书籍,但在应用过滤器时遇到问题。似乎我想要两个 Expand() 方法,但这不起作用。以下查询不起作用,但显示了我正在尝试执行的操作:

var query = from book in ODataContext.Books.Expand("BookAuthorJoins").Expand("Authors")
            where book.BookAuthorJoins.Author.AuthorID = authorID
            select book;

I've got a custom OData feed that for books. Each book can have multiple authors and an author can be involved in multiple books so I implemented this using a join table (Book - BookAuthorJoin - Author). My proxy object has Book.BookAuthorJoins BookAuthorJoin.Books & BookAuthorJoin.Authors.

What I want todo is have a single query where I get all the books for an author in a single LINQ query, but having trouble applying the filter. Seems I want two Expand() methods, but that isn't working. The following query doesn't work, but shows what I'm trying to do:

var query = from book in ODataContext.Books.Expand("BookAuthorJoins").Expand("Authors")
            where book.BookAuthorJoins.Author.AuthorID = authorID
            select book;

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

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

发布评论

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

评论(1

醉生梦死 2024-10-14 17:32:39

在服务器端,一对多或多对多关系通常仅作为导航属性公开,暴露中间的联接表将使您的生活变得更加困难。如果您使用 EF,您应该能够隐藏表并仅将关系公开为导航属性。
无论如何,要获取某个作者的所有书籍,查询应该如下所示:

/Authors(123)/Books

该查询的结果只是书籍的提要。

如果您确实保持连接表公开,那么类似这样的事情可能会起作用:

/Authors(123)/BookAuthorJoins?$expand=Book

但是这次您也获得了所有 BookAuthorJoins 以及每个 BookAuthorJoins 。

On the server side, the 1-to-many or many-to-many relationship is usually exposed as just a navigation property, exposing the join table in the middle will make your life much harder. If you use EF you should be able to hide the table and just expose the relationship as a navigation property.
In any case, to get all books for a certain author the query should look like:

/Authors(123)/Books

The result of this query is just a feed of books.

If you do keep the join table exposed then something like this migth work:

/Authors(123)/BookAuthorJoins?$expand=Book

But this time you get all the BookAuthorJoins with the Book for each as well.

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