LINQ:具有通用属性和值的动态 [英] LINQ: Dynamic where with generic property and value
本文介绍了LINQ:具有通用属性和值的动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图建立一个简单的select方法来查询Linq-to-SQL表,通过一个通用的静态方法。我被困在创建动态表达式。
同样的问题在这里: LINQ:动态选择
- T 是数据库表的类(Person)
- P 是值的类型。值和属性类型应该相同(F.ex字符串)
- 列是给定类的属性名称(Name li>
- value 是字段的Where语句值(Jack)
选择所有名称为Jack= Person.Name =Jack的人员
public static List< T> selectBy< T,P>(String column,P value)其中T:class {
try {
//首先根据给定类型解析已用表
Table< T> table = database.GetTable< T>();
//根据给定的列获取属性
PropertyInfo property = typeof(T).GetTypeInfo()。GetDeclaredProperty(column);
// Func< Data,Data> expressionHere
//选择与给定表达式匹配的所有项目
List< T> objectList = table.Where(expressionHere).ToList< T>();
//返回找到的对象的填充列表
返回objectList;
$ b $ catch(Exception ex){
Debug.WriteLine(selectBy,ex.Message.ToString());
返回null;
$ div $解析方案
你可以像这样手工构建表达式:
//首先根据给定类型解析已用表
Table< T> ; table = database.GetTable< T>();
//根据给定的列获取属性
PropertyInfo property = typeof(T).GetTypeInfo()。GetDeclaredProperty(column);
// Func< Data,Data> expressionHere
ParameterExpression lambdaArg = Expression.Parameter(typeof(T));
表达式propertyAccess = Expression.MakeMemberAccess(lambdaArg,property);
表达式propertyEquals = Expression.Equal(propertyAccess,Expression.Constant(value,typeof(P)));
表达式< Func< T,bool>> expressionHere = Expression.Lambda< Func< T,bool>>(propertyEquals,lambdaArg);
//选择与给定表达式匹配的所有项目
List< T> objectList = table.Where(expressionHere).ToList< T>();
//返回找到的对象的实例列表
return objectList;
I'm trying to build a simple select method for querying Linq-to-SQL table thru a generic static method. I'm stuck at creating the expression dynamically.
Same kind of question here: LINQ : Dynamic select
- T is the class of the database table (Person)
- P is the type of the value. The value and property types should be the same (F.ex String)
- column is the name of property on the given class ("Name")
- value is the where statement value for the field ("Jack")
F.ex Select all persons where name is "Jack" = Person.Name = "Jack"
public static List<T> selectBy<T,P>(String column, P value) where T : class {
try {
// First resolve the used table according to given type
Table<T> table = database.GetTable<T>();
// Get the property according to given column
PropertyInfo property = typeof(T).GetTypeInfo().GetDeclaredProperty(column);
//Func<Data,Data> expressionHere
// Select all items that match the given expression
List<T> objectList = table.Where(expressionHere).ToList<T>();
// Return the filled list of found objects
return objectList;
} catch (Exception ex) {
Debug.WriteLine("selectBy", ex.Message.ToString());
return null;
}
}
解决方案
You can construct the expression manually like this:
// First resolve the used table according to given type
Table<T> table = database.GetTable<T>();
// Get the property according to given column
PropertyInfo property = typeof(T).GetTypeInfo().GetDeclaredProperty(column);
//Func<Data,Data> expressionHere
ParameterExpression lambdaArg = Expression.Parameter(typeof(T));
Expression propertyAccess = Expression.MakeMemberAccess(lambdaArg, property);
Expression propertyEquals = Expression.Equal(propertyAccess, Expression.Constant(value, typeof(P)));
Expression<Func<T, bool>> expressionHere = Expression.Lambda<Func<T, bool>>(propertyEquals, lambdaArg);
// Select all items that match the given expression
List<T> objectList = table.Where(expressionHere).ToList<T>();
// Return the filled list of found objects
return objectList;
这篇关于LINQ:具有通用属性和值的动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文