home screen

Search



Number Of Result : 0

Result :


Sunday, November 2, 2008

Filter trên 1 thuộc tính của đối tượng TEntity bất kỳ


//Kiểu được Filter
public enum TextFilterType
{
Contains,
NotContains,
Exactly,
NotEqual,
StarstWith,
NotStarstWith,
EndsWith,
NotEndsWith,
}



//lay về kiểu phủ định của biểu thức
public static string TextFilterType2NotFunction(TextFilterType e)
{
switch (e)
{
case TextFilterType.NotEqual:
case TextFilterType.NotContains:
case TextFilterType.NotStarstWith:
case TextFilterType.NotEndsWith:
return "!";
default:
return string.Empty;
}
}



public static string TextFilterType2Function(TextFilterType e)
{
switch (e)
{
case TextFilterType.Exactly:
case TextFilterType.NotEqual:
return "Equals";
case TextFilterType.Contains:
case TextFilterType.NotContains:
return "Contains";
case TextFilterType.StarstWith:
case TextFilterType.NotStarstWith:
return "StartsWith";
case TextFilterType.EndsWith:
case TextFilterType.NotEndsWith:
return "EndsWith";
}
return string.Empty;
}



//hàm lấy Expression với Property cần được filter
private static System.Linq.Expressions.Expression GetExpressionMember(ParameterExpression param, string sProperty)
{
Debug.Assert(!string.IsNullOrEmpty(sProperty));
String[] sPropeties = sProperty.Split(new char[] { '.' });
System.Linq.Expressions.Expression exp_propertyInfo = MemberExpression.PropertyOrField(param, sPropeties[0]);
if (exp_propertyInfo.Type.IsValueType && exp_propertyInfo.Type.Name.StartsWith("Nullable"))
{
exp_propertyInfo = MemberExpression.PropertyOrField(exp_propertyInfo, "Value");
}
for (int i = 1; i < sProperties.Length ; i ++ )
{
exp_propertyInfo = MemberExpression.Property(exp_propertyInfo, sPropeties[i]);
if (exp_propertyInfo.Type.IsValueType && exp_propertyInfo.Type.Name.StartsWith("Nullable"))
{
exp_propertyInfo = MemberExpression.PropertyOrField(exp_propertyInfo, "Value");
}
}
return exp_propertyInfo;
}




//Phuong thức chính để Lấy Expression<Func<TEntity, bool>>
public static Expression<Func<TEntity, bool>> Filter<TEntity>() where TEntity : class
{
string Value = "pattern"; //text filter
string sFieldName = "HSClass.ClassName"; //Property Name



TextFilterType FilterType = TextFilterType.Contains;
//Test exist value
if (String.IsNullOrEmpty(Value))
return null;

//Build query
string sFunction = TextFilterType2Function(FilterType);
string sNot = TextFilterType2NotFunction(FilterType);

Type type = typeof(string);
MethodInfo methodInfo = type.GetMethod(sFunction, new Type[] { typeof(string) });
ParameterExpression param = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "e");
System.Linq.Expressions.Expression exp_propertyInfo = GetExpressionMember(param, sFieldName);
System.Linq.Expressions.Expression exp_valueInfo = ConstantExpression.Constant(Value, typeof(string));
System.Linq.Expressions.Expression exp_Condition = BinaryExpression.Call(exp_propertyInfo, methodInfo, exp_valueInfo);

if (!string.IsNullOrEmpty(sNot))
exp_Condition = BinaryExpression.Not(exp_Condition);

var args = new ParameterExpression[] { param };

return Expression.Lambda<Func<TEntity, bool>>(exp_Condition, args);
}


Hàm Main :

static void Main(string[] args)
{
//lay Expression
Expression> expression = Filter();
//tra ve Func<...> từ Expression được lấy ở trên
var func = expression.Compile();

//lay dah sach HocSinh
List<HocSinh> list = GetListHocSinhs();

//filter và trả về kết quả
var result = from s in list
where func(s)
select s ;

}


Reference : Tham khảo từ Khanhtl - Developer của công ty Falcon Software

No comments: