我将OData与Web API一起使用以返回以下JSON:
[{"EmployeeID":1,"FirstName":"Nancy","LastName":"Davolio","Title":"Sales Representative","HireDate":"\/Date(704649600000)\/","Territories":[{"TerritoryID":"06897","TerritoryDescription":"Wilton"},{"TerritoryID":"19713","TerritoryDescription":"Neward"}]}
例如,如何过滤JSON以显示属于Wilton领土的项目?
我已经尝试过了,但是似乎没有用:
http://localhost:62559/Home/Read?$filter=Territories/TerritoryDescription eq Wilton
这是我使用存储库模式从数据库返回对象的代码:
[Queryable]
public IQueryable<EmployeeViewModel> Employees
{
get
{
return context.Employees.Select(e => new EmployeeViewModel
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
HireDate = e.HireDate,
Title = e.Title,
Territories = e.Territories.Select(t => new TerritoryViewModel
{
TerritoryID = t.TerritoryID,
TerritoryDescription = t.TerritoryDescription
})
});
}
}
这是返回JSON对象的控制器:
public ActionResult Read()
{
return Json(repository.Employees, JsonRequestBehavior.AllowGet);
}
解决方法:
在阅读与我相关的问题时遇到了以下答案:
Nested filter on Data Transfer Object using OData Wep Api
有趣的是,我以前在SO上搜索时从未遇到过这个问题.否则,我不必问这个问题.无论如何,这种方法对我有用:
http://localhost:62559/Home/Read?$filter=Territories/any(c:%20c/TerritoryDescription eq 'Wilton')