IQueryable的扩展:创建lambda表达式为关键字查询列 [英] IQueryable Extension: create lambda expression for querying a column for a keyword
问题描述
我开始与此示例中的IQueryable的扩展方法上 CodePlex上。
我相信我所需要的就是一个IQueryable的扩展方法去哪儿,其中方法签名如下:
公共静态的IQueryable< T>其中,< T>(这IQueryable的< T>源,串COLUMNNAME,串关键字)
和有效呢(假设T.columnName是字符串类型):
source.Where(p => p.ColumnName.Contains(使用上述CodePlex上例如关键字))
,我想我明白他是如何做到的排序依据的方法提前工作,但我的问题似乎有点更复杂,我不知道如何获得包含(关键词)的一部分工作。
谢谢,
- 埃德
更新:9/13/2010 6:26 PM PST
我想下面的工作,但最终得到NotSupportedException异常(LINQ的表达节点类型调用不支持LINQ到实体)。当我通过计数执行表达式()。 ?任何想法
公共静态的IQueryable< T>其中,< T>(这IQueryable的< T>源,串COLUMNNAME,串关键字)
{
变种类型= typeof运算(T);
VAR财产= type.GetProperty(COLUMNNAME);
如果(property.PropertyType == typeof运算(字符串))
{
变种参数= Expression.Parameter(类型,P);
VAR propertyAccess = Expression.MakeMemberAccess(参数,属性);
VAR SEL = Expression.Lambda<&Func键LT; T,串>>(propertyAccess,参数);
VAR compiledSel = sel.Compile();
返回source.Where(项目=> compiledSel(项目)。载有(关键字));
}
,否则
{
收益来源;
}
}
公共静态的IQueryable< T>其中,< T>(
本的IQueryable< T>源,串COLUMNNAME,串关键字)
{
VAR ARG = Expression.Parameter(typeof运算(T),P);
变种体= Expression.Call(
Expression.Property(阿根廷,COLUMNNAME),
包含,
空,
Expression.Constant(关键词));
VAR谓词= Expression.Lambda<&Func键LT; T,BOOL>>(身体,ARG);
返回source.Where(谓语);
}
I started with the IQueryable extension methods from this example on CodePlex.
What i believe i need is an IQueryable extension method to "Where", where the method signature looks like:
public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword)
and effectively does this (assuming T.columnName is of type string):
source.Where(p => p.ColumnName.Contains("keyword"))
using the above CodePlex example, i think i understand how he got the OrderBy method working, but my problem seems a bit more complex and I don't know how to get the Contains("keyword") part working.
Thanks in advance,
--Ed
Update: 9/13/2010 6:26pm PST
I thought the following would work, but end up getting a NotSupportedException (The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.) when I execute the expression via Count(). Any ideas?
public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword)
{
var type = typeof(T);
var property = type.GetProperty(columnName);
if (property.PropertyType == typeof(string))
{
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var sel = Expression.Lambda<Func<T, string>>(propertyAccess, parameter);
var compiledSel = sel.Compile();
return source.Where(item => compiledSel(item).Contains(keyword));
}
else
{
return source;
}
}
public static IQueryable<T> Where<T>(
this IQueryable<T> source, string columnName, string keyword)
{
var arg = Expression.Parameter(typeof(T), "p");
var body = Expression.Call(
Expression.Property(arg, columnName),
"Contains",
null,
Expression.Constant(keyword));
var predicate = Expression.Lambda<Func<T, bool>>(body, arg);
return source.Where(predicate);
}
这篇关于IQueryable的扩展:创建lambda表达式为关键字查询列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!