为 IQueryable 扩展 Where [英] extend Where for IQueryable

查看:19
本文介绍了为 IQueryable 扩展 Where的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 IQueryableWhere 方法扩展为这样的:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆