让我们相信,我们需要对实体名单提出质询,我们不知道哪些标准相当活跃,实体既有独裁者,也有内部的简单领域。 让我们成为下一个实体——地址(我只留下一个财产作简便)。
public class Address
{
#region Public members
/// <summary>
/// The extra datafield values
/// </summary>
public IDictionary<string, string> DataFieldValues { get; set; }
public string City { get; set; }
#endregion
}
现在,如果我们问一下一个称为“<>City的固定领域,那么我就要执行:
private static Expression<Func<Address, bool>> BuildLambdaForAQueryItem(string caption, string value)
{
ParameterExpression param = Expression.Parameter(typeof(Address), caption);
BinaryExpression body = Expression.Equal(Expression.PropertyOrField(param, caption),
Expression.Constant(value,
typeof(Address).GetProperty(
caption).PropertyType));
return Expression.Lambda<Func<Address, bool>>(body, param);
}
现在请问一下<>Data FieldValue冷却 我也有必要写出一个类似的文件:
x=>x.DataFieldValues.ContatinsKey(key) && DataFieldValues[key]==value What I get with the method below is almost similar but still it does not apply the filter correctly:
private static Expression<Func<Address, bool>> BuildLambdaForAnExtraField(PostedQueryItem queryItem)
{
ParameterExpression dataFields = Expression.Parameter(typeof(Address), "x");
var dictionaryExpression = Expression.PropertyOrField(dataFields, "DataFieldValues");
var keyExists = Expression.Call(dictionaryExpression, "ContainsKey", null, Expression.Constant(queryItem.Caption));
Expression dictionaryAccessExpr = Expression.Property(dictionaryExpression, "Item",
Expression.Constant(queryItem.Caption));
var valueCorresponds = Expression.Equal(dictionaryAccessExpr, Expression.Constant(queryItem.Value));
return Expression.Lambda<Func<Address, bool>>(keyExists, dataFields).And(
Expression.Lambda<Func<Address, bool>>(valueCorresponds, dataFields));
}