I am using projection (explicit loading) to call and filter collections data returned from a child table, with the result exposed via a RESTful Api service. The json is fine but it repeats the child table collections object. Using "AsNoTracking()" method nulls the nested child collections in the json output which isn't right.
Here is my code below and the json output. I need help to stop the child object repeating
public IQueryable<ApiViewModel> getAllActive()
{
//Explicit loading (projection)
var result = db.Markets
.Where(p => p.IsActive == true)
.Select(p => new ApiViewModel()
{
Market = p,
TravelCentres = p.TravelCentres.Where(x => x.IsActive == true)
});
return result;
}
JSON Output:
[
{
"Market": {
"MarketId": "AE",
"Name": "Arabian Emirates",
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
},
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
}
]
I was able to resolve the issue by using eager loading with 3rd party IncludeFilter() extension method. https://github.com/zzzprojects/EntityFramework-Plus/issues