1
如何传递一个实体属性作为LINQ表达式的参数?如何传递一个实体属性作为LINQ表达式的参数?
public DropdownFilter(WhatTypeHere? ABC)
{
// I want to store a selected property here
// ABC must be a property of TEntity
this.ABC = ABC;
}
// I want the class to encapsulate a LINQ query and just parametrize it with a property
public override IQueryable<TEntity> Filter(IQueryable<TEntity> filteredEntityCollection, string value)
{
// ABC is a property of TEntity
return filteredEntityCollection.Where(this.ABC == value);
}
我会用这样的:
new DropdownFilter<Invoice>(invoice => invoice.SomeProperty);
我已经与Expression<Func<TEntity, string>>
各种参数的尝试,但没有成功。这是抱怨
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
这很棒,我相信它会起作用,但是我不知道如何向上传播'TProperty' ......因为我所有的'filters'都是从基类继承的,它被用作另一个对象的列表属性... –
@HristoYankov好吧,你可以删除TProperty,或者使用'string'(但你应该检查'propertyInfo.PropertyType'是字符串),或者你可以使用'object'作为值,并通过'Expression.Convert(Expression.Constant(value),propertyType)将其转换为属性类型' –