extend Where for IQueryable(为 IQueryable 扩展 Where)
问题描述
我需要将 IQueryable 的 Where 方法扩展为这样的:
I need to extend the Where method for IQueryable to something like this:
.WhereEx("SomeProperty", "==", "value")
我不知道这是否可能,但我在 SO 中找到了这个:无法使用 LINQ OrderBy 中的属性名称进行排序
I dont know if this is even possible, but i found this in SO : Unable to sort with property name in LINQ OrderBy
我试过这个答案,看起来很有趣(Ziad 的答案):
I tried this answer and it seems quite interesting (Ziad's answer):
using System.Linq;
using System.Linq.Expressions;
using System;
namespace SomeNameSpace
{
public static class SomeExtensionClass
{
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = Ascending ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
}
}
Where 方法可以做同样的事情吗?
Is It possible to do the same with the Where method ?
谢谢.
更新:
我现在可以设置一个表达式以传递给 Call 方法,但我得到以下异常:类型 'System.Linq.Queryable' 上没有通用方法 'Where' 与提供的类型参数和参数.如果方法是非泛型的,则不应提供类型参数."
I can now set an expression to pass to the Call method, but i get the following exception: "No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic."
这是我的代码:
public static IQueryable<T> WhereEx<T>(this IQueryable<T> q, string Field, string Operator, string Value)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, Field);
var val = Expression.Constant(Value);
var body = Expression.Equal(prop, val);
var exp = Expression.Lambda<Func<T, bool>>(body, param);
Type[] types = new Type[] { q.ElementType, typeof(bool) };
var mce = Expression.Call(
typeof(Queryable),
"Where",
types,
exp
);
return q.Provider.CreateQuery<T>(mce);
}
请注意,我还没有使用 Operator 参数,而是使用 Equal 进行调试.
Notice that i dont use the Operator argument yet, instead i use Equal for debugging purpose.
有人可以帮我解决这个问题吗?
Can someone help me with this please ?
推荐答案
我用一个简化的问题做了另一个帖子.链接是这里
I did another post with a simplified question. the link is Here
public static IQueryable<T> WhereEx<T>(this IQueryable<T> q, string Field, string Operator, string Value)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, Field);
var val = Expression.Constant(Value);
var body = Expression.Equal(prop, val);
var exp = Expression.Lambda<Func<T, bool>>(body, param);
return System.Linq.Queryable.Where(q, exp);
}
这篇关于为 IQueryable 扩展 Where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 IQueryable 扩展 Where
基础教程推荐
- 如果条件可以为空 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
