c# – 使用$expand的OData会由于包装而中断转换操作

前端之家收集整理的这篇文章主要介绍了c# – 使用$expand的OData会由于包装而中断转换操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在遇到与解决 here:相同的问题

但是答案对我来说还不够.首先我不能为我的生活找到HierarchyNodeExpressionVisitor在OData 5.0.0(不是RC1)(或任何地方,这个问题,尝试谷歌搜索).

第二,即使我发现它返回IHttpActionResult还不够好,我需要返回一个类型的PageResult&My MyModel>

返回IHttpActionResult的声明是“处理结果可能不是IQueryable&MyEntity”的事实.一旦使用$expand操作符.

但这对我来说没有意义,因为我认为$expand运算符用于在实体上包含一个导航属性,就像服务器端Include(e => e.RelatedProperty)一样.至少在我的情况下,我只包括已经在实体上的一个属性,所以我不必担心它“可能是别的东西”.

然而,当使用$expand = Department时,我无法将<>()转换为实体类型,因为它不能转换SelectAllAndExpand< MyEntity>到一个MyEntity.

我如何将展开“展开”回原始实体,以便我可以进行投影?

  1. public PageResult<DateSnippetWithDepartmentsviewmodel> GetDatesWithDepartments(ODataQueryOptions<DateSnippet> options)
  2. {
  3. IQueryable query = options.ApplyTo(_context.DateSnippets,new ODataQuerySettings());;
  4.  
  5. //Exception when using $expand.. cannot cast SelectAllAndExpand<DateSnippet> to DateSnippet
  6. List<DateSnippet> dateSnippets = query.Cast<DateSnippet>().ToList();
  7.  
  8. var dateSnippetsviewmodels = (from d in dateSnippets
  9. select new DateSnippetWithDepartmentsviewmodel
  10. {
  11. ...
  12. });
  13.  
  14. var result = new PageResult<DateSnippetWithDepartmentsviewmodel>(
  15. dateSnippetsviewmodels as IEnumerable<DateSnippetWithDepartmentsviewmodel>,Request.GetNextPageLink(),Request.GetInlineCount());
  16.  
  17. return result;
  18. }

解决方法

尝试这个.希望工作,当我们到达枚举器时,结果应该是一个DateSnippet.你以前做的是试图在Linq Expression树中投射.我怀疑在IQueryable Execute中,这是被解析和转换而不是转换.
  1. public PageResult<DateSnippetWithDepartmentsviewmodel> GetDatesWithDepartments(ODataQueryOptions<DateSnippet> options)
  2. {
  3. IQueryable query = options.ApplyTo(_context.DateSnippets,new ODataQuerySettings());;
  4.  
  5. List<DateSnippet> dateSnippets = new List<DateSnippet>();
  6. foreach(DateSnippet item in query)
  7. {
  8. dateSnippets.Add(item);
  9. }
  10.  
  11. var dateSnippetsviewmodels = (from d in dateSnippets
  12. select new DateSnippetWithDepartmentsviewmodel
  13. {
  14. ...
  15. });
  16.  
  17. var result = new PageResult<DateSnippetWithDepartmentsviewmodel>(
  18. dateSnippetsviewmodels as IEnumerable<DateSnippetWithDepartmentsviewmodel>,Request.GetInlineCount());
  19.  
  20. return result;
  21. }

猜你在找的C#相关文章