Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to filter the records based on child table data using odata query. Example I have tables like below. Employee contains empId and Name, EmployeeText is having empId, languageid and Text

Employees
EmpId Name
1 Test
2 Test2

EmployeeText
empId LanguageId Text
1 1 Test
1 2 French Test
2 1 Test2
2 2 French Test2

Im using webapi with OData and Entity framework

Im calling webapi method from client side using http. I need to get the details based on Text I have given as input. For example if I give input as French Test then it should return the result like shown in below. Means based on text table text it should return main table records.

My API is having code like below

public IQueryable<employee> GetEmployee()
{
return DataContext.Employees();
}

What I have tried:

I tried a query like below, but it is filtering only child records as we applied for child table. Not sure how to meet this. Can you guys help me on this.

localhost:12345/odata/employees?$expand(EmployeeText($filter=Text eq 'French Test'))

I got results like below:
{
  "empId": 1,
  "Text" : "Test",
  "EmployeeText":[
   {
     "empId":1,
     "LanguageId":2,
     "Text": "French Test"
   },
  ]
},
{
  "empId": 2,
  "Text" : "Test",
  "EmployeeText":[]
}

My result should be like below
{
  "empId": 1,
  "Text" : "Test",
  "EmployeeText":[
   {
     "empId":1,
     "LanguageId":2,
     "Text": "French Test"
   },
  ]
}

Please let me know if I am not clear.

Thanks in advance!!
Posted
Updated 21-Aug-17 2:51am
v5
Comments
Graeme_Grant 21-Aug-17 8:21am    
What source code? What results are you seeing? Any errors thrown?

We can't see your screen from here. Please press the "Improve question" widget and update your question. If you do not know how, please read this: How to Improve my question - Code Project FAQ[^]
NaVeN Kumar 21-Aug-17 8:35am    
Sorry. I have updated the question. Please let me know if Im not clear.
Graeme_Grant 21-Aug-17 8:54am    
You are only showing the client-side... All the heavy lifting is on the server-side.

Have you set a breakpoint on the server side and stepped through the code to see what it is doing? Is the server seeing the request that you are making? Is your code expecting the required fields? Is your routing configured correctly?

Please re-read my original post.
Richard Deeming 21-Aug-17 10:02am    
Have you tried:
localhost:12345/odata/employees?$filter=Employee/EmployeeText/Text eq 'French Test'

URI Conventions (OData Version 2.0) | 4.5. Filter System Query Option ($filter)[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900