[英]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. 总结一下,我有两个主表: Company和Employees ,它们之间是一对多的关系:雇员属于公司。
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.