微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

原文地址:https://www.jb51.cc/csharp/94230.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐