Breeze解决多值属性查询的方法 [英] Breeze work-around for multi valued property queries

查看:193
本文介绍了Breeze解决多值属性查询的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Breeze进行临时查询.

I'm trying to assemble ad-hoc queries to Breeze.

我有一个Doctor.contact.addresses关系.

I have a physician.contact.addresses relationship.

当我尝试时:

myPred =  new pred('contact.addresses.street1', op.StartsWith, "a");

执行它,我得到:

属性地址"的属性访问的父值不是单个值.属性访问只能应用于单个值."

"The parent value for a property access of a property 'Addresses' is not a single value. Property access can only be applied to a single value."

要尝试解决此问题,我尝试解析这些多关系并将其传递给.withParameters中的微风控制器,如下所示:

To try a work-around I've tried parsing out those many-relationships and am passing it to the breeze controller in .withParameters like this:

var criteriaStr = toManyArray.length ? ko.utils.stringifyJson(toManyArray) : "";
query = query.withParameters({ searchParms: criteriaStr });

其中toManyArray是fieldName:value对的数组.

where toManyArray is an array of fieldName:value pairs.

在控制器端:

[HttpGet]
public IQueryable<Physician> Physician(string searchParms = null)
{
  if (searchParms != null)
  {
    var ser = new JavaScriptSerializer();
    var searchCritAry = ser.Deserialize<String[]>(searchParms);

    foreach (var aryItem in searchCritAry)
    {
      // aryItem looks like this:
      // f_str_street_from_addresses:a
      var predEnt = aryItem.Split(':')[0].Split('_')[4];
      var predField = aryItem.Split(':')[0].Split('_')[2];    
      var predVal = aryItem.Split(':')[1];

      switch (predEnt)
      {
        case "addresses":
          switch (predField)
          {
            case "street":
              //physPool = 
                _contextProvider.Context.Physicians
                .Where(p => p.Contact.Addresses.Any(a => a.Street1.StartsWith(predVal)));
              break;
            case "street2":
             //physPool =  
               _contextProvider.Context.Physicians
                .Where(p => p.Contact.Addresses.Any(a => a.Street2.StartsWith(predVal)));
              break;
          }
          break;
      }
    }
    // here I want to combine the .Where clauses from above with the breeze preds
    return _contextProvider.Context.Physicians;
  }
  return _contextProvider.Context.Physicians;
}

它不起作用,仅使用通过Breeze查询以常规方式传递的谓词返回选择.我看不到如何将过滤后的IQueryable传递给Breeze的_contextProvider.

It's not working and only returning a selection using the predicates that are passed in the normal way through Breeze's query. I don't see how to pass the filtered IQueryable to Breeze's _contextProvider.

谢谢您的建议.

推荐答案

看看Breeze(从1.4.7版开始)对新的任何/所有"支持.可以在此处找到一些示例: http://www.breezejs.com/documentation/query-examples

Take a look at the new "any/all" support in Breeze ( as of version 1.4.7). Some examples may be found here: http://www.breezejs.com/documentation/query-examples

您可以像这样构造查询/谓词:

You can construct your query/predicate like this:

var pred = new Breeze.Predicate('contact.addresses', "any", 'street1', op.StartsWith, "a"); 
var query = EntityQuery.from("Physicians").where(pred);

或者简单地

var query = EntityQuery.from("Physicians")
       .where("contact.addresses", "any", "street1", "startsWith", 'a')

如果您还希望发送联系人和地址信息,则可能需要添加扩展.

You may want to add an expand if you want the contact and address information sent down as well.

query = query.expand("contact.addresses");

如果需要嵌套任何/所有表达式,则可能需要在BreezeController中添加以下配置,以确定允许表达式深入的深度(在下面的示例中为2):

If you have a need for nested any/all expressions you may need to add the following configuration to your BreezeController to determine just how deep you want to allow the expressions to go (2 in the example below):

[BreezeController(MaxAnyAllExpressionDepth = 2)]

这篇关于Breeze解决多值属性查询的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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