使用表达式树生成 LINQ 查询
更新
感谢 Marc 的帮助,AlphaPagedList 类 现已推出如果有人感兴趣的话,请在 CodePlex 上
原始
我正在尝试创建一个表达式树来返回以给定字符开头的元素。
IList<char> chars = new List<char>{'a','b'};
IQueryable<Dept>Depts.Where(x=> chars.Contains(x.DeptName[0]));
我希望将其用于任何 IEnumerable,其中我向属性提供 lamdba 以进行选择,例如:
Depts.Alpha(x=>x.DeptName, chars);
我一直在尝试这样做但一点运气都没有,有什么帮助吗?
public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars)
{
// Compose the expression tree that represents the parameter to the predicate.
ParameterExpression pe = Expression.Parameter(queryableData.ElementType, "x");
ConstantExpression ch = Expression.Constant(chars,typeof(IEnumerable<char>));
// ***** Where(x=>chars.Contains(x.pi[0])) *****
// pi is a string property
//Get the string property
Expression first = Expression.Constant(0);
//Get the first character of the string
Expression firstchar = Expression.ArrayIndex(pi.Body, first);
//Call "Contains" on chars with argument being right
Expression e = Expression.Call(ch, typeof(IEnumerable<char>).GetMethod("Contains", new Type[] { typeof(char) }),firstchar);
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<T, bool>>(e, new ParameterExpression[] { pe }));
// ***** End Where *****
return (queryableData.Provider.CreateQuery<T>(whereCallExpression));
}
Update
Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if anyone is interested
Original
I'm trying to create an expression tree to return elements that start with a given charecter.
IList<char> chars = new List<char>{'a','b'};
IQueryable<Dept>Depts.Where(x=> chars.Contains(x.DeptName[0]));
I want this to be used on any IEnumerable where I provide a lamdba to the property to select on eg:
Depts.Alpha(x=>x.DeptName, chars);
I've been trying this but having no luck at all, any help?
public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars)
{
// Compose the expression tree that represents the parameter to the predicate.
ParameterExpression pe = Expression.Parameter(queryableData.ElementType, "x");
ConstantExpression ch = Expression.Constant(chars,typeof(IEnumerable<char>));
// ***** Where(x=>chars.Contains(x.pi[0])) *****
// pi is a string property
//Get the string property
Expression first = Expression.Constant(0);
//Get the first character of the string
Expression firstchar = Expression.ArrayIndex(pi.Body, first);
//Call "Contains" on chars with argument being right
Expression e = Expression.Call(ch, typeof(IEnumerable<char>).GetMethod("Contains", new Type[] { typeof(char) }),firstchar);
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<T, bool>>(e, new ParameterExpression[] { pe }));
// ***** End Where *****
return (queryableData.Provider.CreateQuery<T>(whereCallExpression));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似于(重新阅读问题后编辑) - 但请注意,
Expression.Invoke
不适用于 3.5SP1 中的 EF(但在 LINQ-to 中没问题) -SQL):Something like (EDITED after re-reading the question) - but note that
Expression.Invoke
doesn't work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL):