Breeze为多价值的财产查询提供工作方法[英] Breeze work-around for multi valued property queries

本文是小编为大家收集整理的关于Breeze为多价值的财产查询提供工作方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试组装ad-hoc查询到微风.

我有一个物理学生.联系.Addresses关系.

当我尝试时:

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

并执行它我得到:

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

尝试解决工作,我试图解析这些许多关系,并将其传递给我的微风控制器如下:

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

TomanyArray是一系列FieldName:值对.

在控制器侧:

[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.

感谢您的任何建议.

推荐答案

看看微风中的新"任何/所有"支持(如1.4.7版本).这里可以找到一些示例: http://www.breezejs.com/documentation/query- apxamples

您可以构建如下所示/谓词:

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')

如果希望联系人和地址信息,您可能希望添加展开.

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

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

[BreezeController(MaxAnyAllExpressionDepth = 2)]

本文地址:https://www.itbaoku.cn/post/1557128.html

问题描述

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

I have a physician.contact.addresses relationship.

When I try:

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

And execute it I get:

"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."

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

where toManyArray is an array of fieldName:value pairs.

on the controller side:

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

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.

Thanks for any suggestions.

推荐答案

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

or simply

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");

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)]