我使用投影(显式加载)来调用和过滤从子表返回的集合数据,并通过RESTful Api服务公开结果。 json很好,但它重复了子表集合对象。使用“AsNoTracking()”方法会使json输出中的嵌套子集合无效。
下面是我的代码和json输出。我需要帮助来阻止子对象重复
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输出:
[
{
"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"
}
]
}
]
答案 0 :(得分:0)
尝试Distinct()方法。
另见上面的链接
在MSDN
答案 1 :(得分:0)
我能够通过使用第三方IncludeFilter()扩展方法的急切加载来解决此问题。 https://github.com/zzzprojects/EntityFramework-Plus/issues