小编emr*_*ran的帖子

使用OData Wep Api在数据传输对象上嵌套过滤器

我有一个wep api项目使用odata消耗数据,但我在使用odata wep api时遇到了一些问题.

当我执行该查询时

/ api/values?$ top = 50&$ filter = Comments/Fortuneteller/FullName eq'some string'

它给了我以下错误

"消息":"URI中指定的查询无效.","ExceptionMessage":"属性'Fortuneteller'的属性访问的父值不是单个值.属性访问只能应用于单个值."

我不想从控制器返回实体对象.有没有办法通过DTO过滤实体?

我在我的项目中使用Repository + Service层模式,我的项目结构就是这样

api controller < - > service < - > repository < - > EF

api控制器

    [Queryable]
    public IQueryable<FortuneDTO> Get()
    {
        return service.FiterBy((_ => true));
    }
Run Code Online (Sandbox Code Playgroud)

服务

    public IQueryable<FortuneDTO> FiterBy(Expression<Func<tblFortune, bool>> filter)
    {
        return repository.List().Where(filter).Select(_ => new FortuneDTO
        {
            CreatedByFullName = _.aspnet_Users.FullName,
            Id = _.FortuneId,
            Comments = _.tblComment.Select(c => new CommentDTO
            {
                Id=c.CommentId,
                Comment = c.Comment,
                Fortuneteller = new FortunetellerDTO …
Run Code Online (Sandbox Code Playgroud)

c# odata asp.net-mvc-4 asp.net-web-api

18
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net-mvc-4 ×1

asp.net-web-api ×1

c# ×1

odata ×1