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. 我如何将展开“展开”回原始实体,以便我可以进行投影? public PageResult<DateSnippetWithDepartmentsViewModel> GetDatesWithDepartments(ODataQueryOptions<DateSnippet> options) { IQueryable query = options.ApplyTo(_context.DateSnippets,new ODataQuerySettings());; //Exception when using $expand.. cannot cast SelectAllAndExpand<DateSnippet> to DateSnippet List<DateSnippet> dateSnippets = query.Cast<DateSnippet>().ToList(); var dateSnippetsViewModels = (from d in dateSnippets select new DateSnippetWithDepartmentsViewModel { ... }); var result = new PageResult<DateSnippetWithDepartmentsViewModel>( dateSnippetsViewModels as IEnumerable<DateSnippetWithDepartmentsViewModel>,Request.GetNextPageLink(),Request.GetInlineCount()); return result; } 解决方法
尝试这个.希望工作,当我们到达枚举器时,结果应该是一个DateSnippet.你以前做的是试图在Linq Expression树中投射.我怀疑在IQueryable Execute中,这是被解析和转换而不是转换.
public PageResult<DateSnippetWithDepartmentsViewModel> GetDatesWithDepartments(ODataQueryOptions<DateSnippet> options) { IQueryable query = options.ApplyTo(_context.DateSnippets,new ODataQuerySettings());; List<DateSnippet> dateSnippets = new List<DateSnippet>(); foreach(DateSnippet item in query) { dateSnippets.Add(item); } var dateSnippetsViewModels = (from d in dateSnippets select new DateSnippetWithDepartmentsViewModel { ... }); var result = new PageResult<DateSnippetWithDepartmentsViewModel>( dateSnippetsViewModels as IEnumerable<DateSnippetWithDepartmentsViewModel>,Request.GetInlineCount()); return result; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |