简体   繁体   English

从一对多关系中检索实体(Odata)

[英]Retrieving entities from a one to many relationship (Odata)

Summarizing, I have two main tables: Company and Employees , with a one-to-many relationship between them: employees belongs to a company. 总结一下,我有两个主表: CompanyEmployees ,它们之间是一对多的关系:雇员属于公司。

The Company entity has a property called Employees, which allows to get the employees who belongs to the specific Company. 公司实体具有一个名为“员工”的属性,该属性允许获取属于特定公司的员工。

If I type in the browser this URL, it works and I get an employees list: 如果我在浏览器中键入此URL,它将起作用,并且我会获得一个雇员列表:

http://domain.com/DynamicsNAV80/OData/Company('whatever')/Employees

Now, I want to retrieve the employees using a Linq query, how can I do it? 现在,我想使用Linq查询来检索员工,该怎么办?

I have tried this: 我已经试过了:

var dataServiceQuery = (DataServiceQuery<Company>)from comp in _context.Company.Expand(comp => comp.WhseEmployee)
                                                  where comp.Name == "whatever"
                                                  select comp.WhseEmployee;

But this is not working for me. 但这对我不起作用。

What does that query return, an error or just not the data your looking for? 该查询返回什么,错误或者不是您要查找的数据? Im not sure if the syntax for querying Odata is different but this is how i would do it any other time. 我不确定查询Odata的语法是否不同,但这是我在其他时间会做的事情。

              var dataServiceQuery = from comp in _context.Company.Expand("WhseEmployees")
                                     where comp.Name == "whatever"
                                     select comp;

Which version of OData are you using? 您正在使用哪个版本的OData?

If it is V4. 如果是V4。 You can try following code. 您可以尝试以下代码。

var employees = _context.Company.ByKey("whatever").WhseEmployee;

Please refer to Client Delayed Query 请参考客户端延迟查询

If it is V3. 如果是V3。 You need to query the company first, and then use LoadProperty to send request to /Company('whatever')/WsheEmployee. 您需要先查询公司,然后使用LoadProperty将请求发送到/ Company('whatever')/ WsheEmployee。

var company = _context.Company.Where(c=>c.Name="whatever").First();
dsc.LoadProperty(company, "WsheEmployee");

Finally I could do with this query: 最后,我可以使用以下查询:

    var dataServiceQuery = (DataServiceQuery<WhseEmployee>)_context.Company.Where(c => c.Name == companyName)
                                                                           .SelectMany(c => c.WhseEmployee);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除一对多关系中的相关实体 - deleting related entities in a one-to-many relationship 不记得之前为一对多关系添加的实体的实体列表 - List of entities not remembering previously added entities for a one-to-many relationship 如何在OData Client中处理一对多关系? - How to handle One-to-Many relationship in OData Client? 创建具有多对多关系的实体副本,而无需复制其中一种类型 - Creating copy of entities with many to many relationship without duplicating one of the type EF多对多关系未添加实体之一的记录 - EF many to many relationship is not adding records for one of the entities 如何在EF中指向来自不同实体的同一实体的一对多关系建模 - How to Model One to Many relationship pointing to same entity from different entities in EF 从实体框架中删除具有多对多关系的对象? - Delete an object with a many to many relationship from the entities framework? 如何从多对多关系中检索实体列表? - How can I retrieve a List of entities from a many to many relationship? 使用EF 6 MVC 5并从多对多关系中检索数据 - Working with EF 6 MVC 5 and retrieving data from many-to-many relationship 删除多对多关系中的实体 - Delete Entities in Many to Many Relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM