Flter扩展桌子,微风 [英] Flter expanded table with breeze

查看:215
本文介绍了Flter扩展桌子,微风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用角,微风,WebApi,EF,SqlServer2008。
我有表文章和表文章评论,所以一篇文章可以有很多articleComments记录。

  public class Article {

public int ArticleId {get;组; }
public string ArticleName {get;组; }
public string描述{get;组; }
public ICollection< ArticleComment>评论{get;组; }
public ICollection< ArticleImage> ImagesList {get;组; }
...
}

public class ArticleComment
{
public int ArticleCommentId {get;组; }
public string CommentText {get;组; }
public int UserId {get;组; }
public int Rate {get;组; }
public DateTime CommentDate {get;组; }
public int ArticleId {get;组; }
public Article Article {get;组; }
public int Status {get;组;
}

在客户端我需要获得完整的文章实体与评论,图像和其他链接的实体,但注释只需要选定的文章记录,其中字段status== 1。



我尝试使用这样的查询

  var pred = breeze.Predicate.create('articleId','eq',id)
.and('comments',' ','status','==',1);
return EntityQuery.from(Articles)
.where(pred)
.expand('comments,imagesList ...')
.toType(entityNames.article)
.using(manager).execute()
.to $ q(querySucceded,self._queryFailed);

这将返回文章的所有注释,但不会按状态字段过滤扩展表ArticleComments。 >

解决方案

实体框架不支持过滤时使用.Include(...)这是什么微风的.expand(... )转换为服务器。类似的问题发布在这里 here



您当前的查询是:


只要具有至少一个
注释,状态为== 1,请给我articleId == id的文章。包括所有的评论和图片。


我相信你想表达的是:


给我带articleId == id的文章。包含其状态为== 1
及其所有图片的评论。


我不知道一种表达方式这在一个查询中,除非您创建一个类似于Jay描述的专用控制器操作此处



或者你可以这样做:

  var pred = breeze.Predicate.create('articleId ','eq',id); 
return EntityQuery.from(Articles)
.where(pred)
.expand('comments,imagesList')
.toType(entityNames.article)
。$($)
.execute()
.to $ q(
函数(结果){
//分离所有注释的状态!= 1
var commentType = manager.metadataStore.getEntityType(entityNames.articleComment),
comments = manager.getEntities(commentType)
.filter(function(comment){return comment.status!== 1;});
comments.forEach(manager.detachEntity);
querySucceded(result);
},
self._queryFailed);

这种方法的上升是不需要服务器端修改。下载是加载更多的意见,而不是强制您在客户端分离他们。



第三种方法是发出两个查询:一个加载文章它的imageList和第二个查询来加载其articleId == id和status == 1的注释。为此,您需要在控制器中执行一个返回 IQueryable< ArticleComment> 的操作。这种方法的缺点是您需要发出两个http请求。你可以并行地做到这一点,随着spdy的出现,长远来看可能并不是那么大。


I am using in my project angular, breeze, WebApi, EF, SqlServer2008. I have table Articles and table ArticleComments, So one article can have many articleComments records.

public class Article{

    public int ArticleId { get; set; }
    public string ArticleName { get; set; }
    public string Description { get; set; }
    public ICollection<ArticleComment> Comments { get; set; }
    public ICollection<ArticleImage> ImagesList { get; set; }
    ...
}

public class ArticleComment
{
    public int ArticleCommentId { get; set; }
    public string CommentText { get; set; }
    public int UserId { get; set; }
    public int Rate { get; set; }
    public DateTime CommentDate { get; set; }
    public int ArticleId { get; set; }
    public Article Article { get; set; }
    public int Status { get; set; }
}

in client I need to get full Article entity with comments, images and others linked entities, But comments need to be only for selected article record and where field "status" == 1.

I try to use such query

          var pred = breeze.Predicate.create('articleId', 'eq', id)
                  .and('comments', 'any', 'status', '==', 1);
          return EntityQuery.from("Articles")
            .where(pred)
            .expand('comments, imagesList...')
            .toType(entityNames.article)
            .using(manager).execute()
            .to$q(querySucceded, self._queryFailed);

this returns all comments for article , but does not filter expanded table ArticleComments by status field.

解决方案

Entity Framework does not support filtering when using ".Include(...)" which is what breeze's ".expand(...)" translates to on the server. Similar questions posted here and here.

Your current query is saying:

Give me the article with articleId==id as long as it has at least one comment with status==1. Include all of it's comments and images.

I believe what you want to express is:

Give me the article with articleId==id. Include comments whose status==1 and all of it's images.

I don't know of a way to express this in one query unless you create a dedicated controller action similar to what Jay describes here.

Alternatively you could do something like this:

var pred = breeze.Predicate.create('articleId', 'eq', id);
return EntityQuery.from("Articles")
    .where(pred)
    .expand('comments, imagesList')
    .toType(entityNames.article)
    .using(manager)
    .execute()
    .to$q(
        function(result) {
            // detach all comments whose status!=1
            var commentType = manager.metadataStore.getEntityType(entityNames.articleComment),
                comments = manager.getEntities(commentType)
                     .filter(function(comment) { return comment.status !== 1; });
            comments.forEach(manager.detachEntity);
            querySucceded(result);
        },
        self._queryFailed);

Upside of this approach is it doesn't require server-side modifications. Downside is it loads more comments than you need and forces you to detach them on the client afterwards.

A third approach would be to issue two queries: one to load the article and it's imageList and a second query to load the comments whose articleId==id and status==1. To do this you would need to have an action in your controller that returns an IQueryable<ArticleComment>. Downside of this approach is you'd need to issue two http requests. You could do these in parallel and with the advent of spdy this may not be such a big deal in the long run.

这篇关于Flter扩展桌子,微风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆