通用的扩展方法Queryable,当将表达式转换为Linq-to-entities可用的内容时,在Invoke上崩溃 [英] Generic extension method on Queryable, Crashes on Invoke when converting expressions to something usable by Linq-to-entities

查看:168
本文介绍了通用的扩展方法Queryable,当将表达式转换为Linq-to-entities可用的内容时,在Invoke上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个通用的扩展方法,用于解析查询字符串或路由数据参数,并根据该参数过滤某些可查询。

I wrote a generic extension method that takes the pain out of parsing a querystring or route data parameter and filtering a certain queryable based on that parameter.

  public static IQueryable VaryBy<T>(this IQueryable<T> input,Func<T,int> navigationalproperty,string routeparameter,PageController currentPagecontroller) where T : EntityObject
    {
        string navid = currentPagecontroller.GetFromRequestOrRouteData(routeparameter);
        if (!String.IsNullOrEmpty(navid))
        {
            int navidasint = int.Parse(navid);
            //return input.Where(x => navigationalproperty(x) == navidasint);

            return (from var in input
                    let result = navigationalproperty(var)
                    where result == navidasint
                    select var);
        }
        else
        {
            return input;
        }
    }

这个工作,会让我写在我的控制器中使用干净的代码行,而不必每次复制粘贴上述块的非泛型版本。

this , when working, would allow me to write the following clean line of code in my controllers, instead of having to copy paste the non-generic version of the above block every time.

 return aDB.GetAll<Product>().VaryBy(x=> x.Category.Id, "categoryid", this);

可惜的是,在结果调用ToList()时会抛出异常:LINQ表达式LINQ to Entities不支持节点类型Invoke。

Sadly this throws an exception when calling ToList() on the result: "The LINQ expression node type ‘Invoke’ is not supported in LINQ to Entities."

我明白为什么会发生这种情况。我也读过我可以使用 LinqKit 的AsExpandable为此,但我有点犹豫,使用他们的自动解决方案之前,我明白我可以使自己的工作,
我开放的方案使用不同的方法,只是试图在这里学习

I kind of understand why this is happening. I also read that I could maybe use LinqKit's AsExpandable for this, but I'm kind of hesitant to use their "automatic solution" before I understand how I could make this work myself, I'm open to solutions that use a different approach aswell, just trying to learn here!

推荐答案

感谢约翰·斯凯特 answer 在我的问题上简化了这一个,我成功地创建了我的VaryBy函数,如下所示:

Thanks to John Skeet's answer on my question wich simplified this one, I succeeded at creating my VaryBy function like this:

public static IQueryable VaryBy<T>(this IQueryable<T> input,Expression<Func<T,int>> navigationalproperty,string routeparameter,PageController currentPagecontroller) where T : EntityObject
    {
        string navid = currentPagecontroller.GetFromRequestOrRouteData(routeparameter);
        if (!String.IsNullOrEmpty(navid))
        {
            int navidasint = int.Parse(navid);
            Expression equals = Expression.Equal(navigationalproperty.Body, Expression.Constant(navidasint));
            return input.Where(Expression.Lambda<Func<T, bool>>(equals, navigationalproperty.Parameters));

        }
        else
        {
            return input;
        }
    }

这篇关于通用的扩展方法Queryable,当将表达式转换为Linq-to-entities可用的内容时,在Invoke上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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