如何筛选子集合使用LINQ动态 [英] How to filter child collection with linq dynamic
问题描述
我尝试过滤用户请求的结果。
例如,您有订单
和订单详情
和产品
是子集合。
I'm trying to filter results for user request.
For instance you have orders
and order details
and products
is child collection.
当用户想要按产品过滤时,我收到一个错误,因为没有属性或字段'PRODUCTS'存在于类型'ICollection
1'`
When user wants to filter by product I'm getting an error because of No property or field 'PRODUCTS' exists in type 'ICollection
1'`
我写这样的查询。
var orders = _uow.Repository<ORDERS>()
.Query()
.Where("PRODUCTS.HEADING.ToLower().Contains(\"foo\")")
.Include("ORDER_DETAILS")
.Include("ORDER_DETAILS.PRODUCTS")
.ToList();
因此,不可能像这样过滤子集合
So it's not possible to filter child collection like this? Or any way to filter?
感谢。
推荐答案
方式你命名你的类/属性,很难猜到哪一个是一个单一的对象,哪一个是一个集合属性。
From the way you named your classes/properties it's hard to guess which one is a single object and which one is a collection property.
如果 ORDERS
class属性 ORDER_DETAILS
ORDER_DETAILS
类和 ORDER_DETAILS
类属性产品
产品
类的类的
HEADINGS
,那么下面应该做的技巧:
If ORDERS
class property ORDER_DETAILS
is a collection of ORDER_DETAILS
class, and ORDER_DETAILS
class property PRODUCTS
is a singe object of PRODUCTS
class having a string property HEADINGS
, then the following should do the trick:
.Where("ORDER_DETAILS.Any(PRODUCTS.HEADING.ToLower().Contains(\"foo\"))")
基本上与使用lambda参数的静态查询相同
It's basically the same as static query with lambda parameters skipped
.Where(o => o.ORDER_DETAILS.Any(d => d.PRODUCTS.HEADING.ToLower().Contains("foo")))
这篇关于如何筛选子集合使用LINQ动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!