Dynamics CRM OData query filtering on expanded attributes only works if no results come out?

故事扮演 提交于 2019-12-11 10:12:33

问题


I have a requirement to fetch Price List Item records which adhere to the following requirements:

  1. Filter by a specific PriceList
  2. Filter by a specific currency
  3. Filter by the Name of the related Product containing a given string

I got the first two points working no problem, but it feels like expanding doesn't cope well with filtering. I started from a "straight" query on Product entity:

.../ProductSet?$filter=substringof('sometext', Name)

Equivalent SQL (targeting the corresponding CRM filtered views for clarity):
SELECT * FROM FilteredProduct WHERE ProductNumber LIKE '%sometext%'

The above query works, I can tweak it and have no issues. However, if I attempt to move on to ProductPriceLevel (thus expanding the relationship with Product, which is product_price_levels) I end up with this:

.../ProductPriceLevelSet?$expand=product_price_levels&$filter=substringof('sometext', product_price_levels/Name)

Equivalent SQL (again, targeting the relevant filtered views):
SELECT * FROM FilteredProductPriceLevel PPL JOIN FilteredProduct P 
    ON PPL.ProductId = P.ProductId WHERE P.ProductNumber LIKE '%sometext%'

Which has two different outcomes I see:

  • If the $filter has no matches, it works fine and returns an empty result set
  • If the $filter matches something, I get an error

code: -2147220970

message: The result selector of the 'Join' operation must return an anonymous type of two properties.

AFAIK that's what happens when you hit a limitation of LINQ-to-CRM regarding using .Where() on multiple entities at once... doesn't seem relevant!

What's wrong with my query ?

NOTE: The CRM 2013 I'm using is On-Premise, without any update rollup / service pack.

ALSO NOTE: The equivalent SQL, as can be expected, works perfectly


回答1:


I don't think CRM OData supports adding a filter on a joined entity. Try reversing the entity you're actually starting with, and add a path to the referencing entity:

ProductSet()?$filter=substringof('sometext',ProductNumber)&$expand=product_price_levels&$select=product_price_levels/*

P.S. If you have linqPad, this is the query I used to generate this:

from p in ProductSet
where p.ProductNumber.Contains("sometext")
select new { p.product_price_levels }


来源:https://stackoverflow.com/questions/25785485/dynamics-crm-odata-query-filtering-on-expanded-attributes-only-works-if-no-resul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!