使用LINQ进行双重嵌套的OData收集 [英] Use LINQ for double-nested OData collection

查看:50
本文介绍了使用LINQ进行双重嵌套的OData收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想要做的是有一个查询,在一个LINQ查询中我可以找到所有作者的书,但是在应用过滤器时遇到了麻烦.似乎我想要两个Expand()方法,但这不起作用.以下查询不起作用,但显示了我要执行的操作:

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;

推荐答案

在服务器端,通常将一对多或多对多关系公开为一个导航属性,从而在中间会让你的生活更加艰难.如果您使用EF,则应该能够隐藏表并将该关系作为导航属性公开. 无论如何,要获取某位作者的所有书籍,查询应类似于:

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

但是这一次,您还获得了每个Book的所有BookAuthorJoins.

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

这篇关于使用LINQ进行双重嵌套的OData收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆