将PredicateBuilder与WebAPI OData服务和EntityFramework一起使用

时间:2014-06-18 12:56:23

标签: c# entity-framework asp.net-web-api odata predicatebuilder

我正在为单页面应用程序使用OData查询功能。我最近尝试根据用户权限在业务层添加一些过滤,这导致我的odata查询在某些情况下失败。

我的OData控制器引用业务层中的一个类,该类返回IQueryable。业务层引用实体框架数据库第一模型。

在我的业务层中,我有以下属性。

public IQueryable<Job> Jobs
{
    get
    {
        IQueryable<Job> queryable = UnitOfWork.Jobs.AsExpandable();

        var predicate = PredicateBuilder.False<Job>();

        foreach (int clientID in userService.GetAllowedClientIds())
        {
            int temp = clientID;
            predicate = predicate.Or(p => p.ClientId == temp);
        }

        return queryable.Where(predicate);
        // I've tried returning this as .AsExpandable() again and as .AsQueryable() and neither change the result.
    }
}

我的odata控制器是一个简单的传递给BL。

[Queryable(MaxExpansionDepth = 3)]
public IQueryable<Job> Get()
{
    return jobService.Jobs;
}

现在,根据我在URL中传递的查询参数,我获取数据或"Cannot compare elements of type 'System.Collections.Generic.ICollection 1 [[AssociatedType]]'。只支持原始类型,枚举类型和实体类型。“`

例如,/Jobs?$expand=LatestJobAllocations/Gang工作正常但/Jobs?$expand=JobLocationStructures失败。区别在于LatestJobAllocations是我添加到模型中的视图,而JobLocationStructures是一个表格,JobLatestJobAllocations也是1-0..1关系。

如果我向失败的请求添加分页奇迹般地起作用。 /Jobs?$expand=JobLocationStructures&$inlinecount=allpages&$top=25&$skip=0这将是很好的,除了我不能使用分页来获取单个对象/Jobs(1234)?$expand=JobLocationStructures&$inlinecount=allpages&$top=25&$skip=0失败,因为在单个实体端点上不允许分页。

0 个答案:

没有答案
相关问题