如何使用通用列表处理整数或日期时间 [英] how to handle integer or Datetime with Generic List
问题描述
我想创建动态列表我想从列表中过滤项目
这个函数完全适用于字符串,但除了像Int32或DataTime之类的字符串它给出异常
I am tring to create dynamic list i want to filter items from list
this function perfectly work with string but other than string like Int32 or DataTime it gives exception
public static List<T> Filter<T>
(this List<T> source, string columnName,
string compValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "x");
Expression property = Expression.Property(parameter, columnName);
Expression constant = Expression.Constant(compValue);
if (property.Type == typeof(string)) //this is for testing
{
Expression<Func<T, bool>> predicate =
GetExpression<T>(columnName, compValue);
Func<T, bool> compiled = predicate.Compile();
return source.Where(compiled).ToList();
}
return null;
}
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue); // on this line i am getting error if i remove if condition of type checking
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
错误
声明在'System.String'类型上无法使用'System.Int32'类型的实例调用c#
所以我想要一些可以处理这个问题的函数
实际上我想在mvc中创建数据表Jquery的动态列表
http ://www.datatables.net/ [ ^ ]
i卡在我的一个列上是整数所以我无法在该列上过滤
所以我在搜索
Error
declared on type 'System.String' cannot be called with instance of type 'System.Int32' c#
so i want some function that can handle this issue
actually i tring to create dynamic list for datatable Jquery in mvc
http://www.datatables.net/[^]
i am stuck on one of my column is integer so i am not able to filter on that column
so i am searching
推荐答案
我现在改变它按预期工作
我想要的字符串
除了字符串我想要等于
I change this now it working as expected
for string i want like
other than string i want equal
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
if (propertyExp.Type == typeof(string))
{
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
else
{
var converter = TypeDescriptor.GetConverter(propertyExp.Type);
var result = converter.ConvertFrom(propertyValue);
var someValue = Expression.Constant(result);
var containsMethodExp = Expression.Equal(propertyExp, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
}
这篇关于如何使用通用列表处理整数或日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!