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)將其轉換爲屬性類型' –