我最近从使用Linq切换到Sql到实体框架。我一直在努力的一件事是获得一个通用的IQueryable扩展方法,该方法是为Linq to Sql与实体框架一起工作而构建的。这个扩展方法依赖于SqlMethods的Like()方法,它是Linq to Sql特有的。我真的很喜欢这种扩展方法,它允许我通过简单地传入一个属性名称(字符串)和一个查询子句(也作为字符串),在运行时在任何对象上动态构造一个Sql Like语句。这种扩展方法对于使用flexigrid或jqgrid等网格非常方便。这里是Sql的Linq版本(从本教程采取:http://www.codeproject.com/KB/aspnet/MVCFlexigrid.aspx):转换依赖SqlMethods.Like()用于实体框架的Linq表达式树
public static IQueryable<T> Like<T>(this IQueryable<T> source,
string propertyName, string keyword)
{
var type = typeof(T);
var property = type.GetProperty(propertyName);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var constant = Expression.Constant("%" + keyword + "%");
var like = typeof(SqlMethods).GetMethod("Like",
new Type[] { typeof(string), typeof(string) });
MethodCallExpression methodExp =
Expression.Call(null, like, propertyAccess, constant);
Expression<Func<T, bool>> lambda =
Expression.Lambda<Func<T, bool>>(methodExp, parameter);
return source.Where(lambda);
}
有了这个扩展方法,我可以简单地做到以下几点:
someList.Like( “名字”, “迈克” );
或
anotherList.Like(“ProductName”,“widget”);
有没有一种等同的方式来使用实体框架做到这一点?
在此先感谢。
可查询的搜索看起来不错。感谢您分享链接。 – 2010-04-02 04:58:45