【发布时间】:2017-11-13 00:29:06
【问题描述】:
我有一个动作,它会从数据库中返回有限的商店物品列表,这里是下面的代码
[HttpGet]
public IEnumerable<ProductInfo> GetTop(int amount = 5)
{
var mostPopulardProductIds = dbe.SalesOrderDetail.GroupBy(p => p.ProductID)
.OrderByDescending(p => p.Count())
.Take(amount)
.Select(p => p.Key)
.ToList();
var result = dbe.Product
.Where(p => mostPopulardProductIds.Contains(p.ProductID))
.Select(p => new ProductInfo()
{
Id = p.ProductID,
Name = p.Name
}
).ToList();
return result;
}
这是我的 ProductInfo 类
[DataContract]
public class ProductInfo
{
public int Id { get; set; }
public string Name { get; set; }
//public
}
但是当我尝试通过浏览器访问它时,我得到一个空对象列表,看起来像第一张图片
但调试器向我显示,结果变量包含带有数据的普通对象
我尝试通过Request.CreateResponse 方法返回结果,但这仍然不起作用
【问题讨论】:
-
能否在 ProductInfo 类变量中添加 datamember 属性?
-
@Naruto,是的,谢谢,现在可以了,现在我的问题看起来很愚蠢:D。但是非常感谢,伙计
标签: c# asp.net-web-api serialization