我有这些课程:
public class Items
{
[Key]
public Guid Id { get; set; }
public string ItemCode { get; set; }
public decimal SalesPriceExcl { get; set; }
public decimal SalesPriceIncl { get; set; }
public virtual ICollection<ItemPrice> SalesPrices { get; set; }
public Items()
{
SalesPrices = new HashSet<App4Sales_ItemPrice>();
}
}
public class ItemPrice
{
[Key, Column(Order = 0), ForeignKey("Items")]
public Guid Id { get; set; }
public virtual Items Items { get; set; }
[Key, Column(Order=1)]
public Guid PriceList { get; set; }
public decimal PriceExcl { get; set; }
public decimal PriceIncl { get; set; }
public decimal VatPercentage { get; set; }
}
我想查询Items并自动获取ItemPrice集合。
我创建了一个OData V3控制器:
// GET: odata/Items
//[Queryable]
public IQueryable<Items> GetItems(ODataQueryOptions opts)
{
SelectExpandQueryOption expandOpts = new SelectExpandQueryOption(null, "SalesPrices", opts.Context);
Request.SetSelectExpandClause(expandOpts.SelectExpandClause);
return expandOpts.ApplyTo(db.Items.AsQueryable(), new ODataQuerySettings()) as IQueryable<Items>;
}
但我收到错误: “无法序列化null feed”
是的,有些项目没有ItemPrice列表。
我可以解决这个错误,还是可以做一些不同的事情?
亲切的问候
的Jeroen
我发现潜在的错误是:
无法转换类型的对象
'System.Data.Entity.Infrastructure.DbQuery 1[System.Web.Http.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand
1 [.Models.Items]]'
输入'.Models.Items'。
答案 0 :(得分:0)
GetItems([FromODataUri] ODataQueryOptions queryOptions)
答案 1 :(得分:0)
我遇到这篇文章后就解决了这个问题:http://www.jauernig-it.de/intercepting-and-post-processing-odata-queries-on-the-server/
这是我的控制器:
SelectExpandQueryOption expandOpts = new SelectExpandQueryOption(null, "SalesPrices", opts.Context);
Request.SetSelectExpandClause(expandOpts.SelectExpandClause);
var result = expandOpts.ApplyTo(db.Items.AsQueryable(), new ODataQuerySettings());
var resultList = new List<Items>();
foreach (var item in result)
{
if (item is Items)
{
resultList.Add((Items)item);
}
else if (item.GetType().Name == "SelectAllAndExpand`1")
{
var entityProperty = item.GetType().GetProperty("Instance");
resultList.Add((Items)entityProperty.GetValue(item));
}
}
return resultList.AsQueryable();
的Jeroen