如果您已有ProductCount您的供应商实体的财产,您只需要从服务器返回它。如果ProductCount是在服务器端实体填充,您可以在服务器和客户端的属性将只是工作:
[HttpGet]
public IQueryable<Supplier> SuppliersWithProductCount()
{
var query = _repository.Suppliers.AsQueryable();
// ...add server-side params to the query, if desired...
// Get objects with the supplier and the count
var list = query.Select(s => new { Supplier = s, Count = s.Products.Count }).ToList();
// Set the count on the Supplier
list.ForEach(o => o.Supplier.ProductCount = o.Count);
// Extract the Suppliers
var suppliers = list.Select(o => o.Supplier);
return suppliers.AsQueryable();
}
在另一方面,如果ProductCount财产只有客户端上存在,您需要将其分别传递给客户端,并在查询结果处理程序中将客户端实体的属性设置为。
在服务器上:
[HttpGet]
public IQueryable<Object> SuppliersWithProductCount()
{
var query = ContextProvider.Context.Suppliers.AsQueryable();
// ...add server-side params to the query, if desired...
// Get objects with the supplier and the count
return query.Select(s => new { Supplier = s, Count = s.Products.Count });
}
在客户端:
EntityQuery.from("SuppliersWithProductCount")
.using(myEntityManager).execute()
.then(function (data) {
// results are { Supplier, Count } objects
var results = data.results;
var suppliers = [];
results.forEach(function (r) {
r.Supplier.ProductCount = r.Count;
suppliers.push(r.Supplier);
});
return suppliers;
});
你可以尝试添加该服务器端的计算性能,以实体的模型,然后添加属性的实体构造(不是初始化程序)客户端,所以计算的属性从服务器返回。 – Jonathan