使用实体法的结果与lambda表达式或LINQ查询过滤 [英] Use result of entity's method for filtering with lambda expression or linq query

查看:123
本文介绍了使用实体法的结果与lambda表达式或LINQ查询过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这取决于他们的工作性质的函数的结果过滤我的entites。

I would like to filter my entites depending on result of a function working with their properties.

IE浏览器。我得到的实体是这样的:

ie. I got entity like this:

public class Lorem
{
    public int A {get;set;}
    public int B {get;set;}
    public int C {get;set;}

    public double DoMath(int externalValue)
    {
        // real implementation is longer and more complex

        if(A==B) {
          // some calculations
          return 0.2;
        }
        if(B==C) {
          // some calculations
          return 0.9;
        }
        else return 0;
    }
}

现在我查询实体,我想获得只有那些具有DoMath> 0

Now I am querying entities and I would like to get only those with DoMath > 0.

// simplified scenario for example purpose
int someValue = 95;
// working with Entity Framework 4.1 Code First
var filtered = db.Lorems.Where(x=>x.DoMath(someValue) > 0);



我收到此错误:结果
LINQ到实体不识别方法-----,这种方法不能被翻译成店的表情。

是否有可能使这样的工作吗?< 。BR>
我定制lambda表达式,并与代表们的工作技能是相当的差,所以我想获得一些帮助。

Is it possible to make it work like this?
My skills in customizing lambda expressions and working with delegates is quite poor, so I would like to get some help.

编辑:这是一个 DoMath功能,真正的代码(不包括注释,因为它们不是英文):

Here is that 'DoMath' function real code (without comments, because they aren't in english):

public double VypocitejPrislusnost(int value)
{
    if (A == B)
    {

        if (value <= C)
        {
            return 1;
        }
        if (value > D)
        {
            return 0;
        }
        else
        {
            double b = D - C;
            double tangens = Math.Tan(1 / b);

            double a = tangens * (D - value);

            return Math.Round(a, 2);
        }
    }

    if (C == D)
    {

        if (value >= B)
        {
            return 1;
        }
        if (value <= A)
        {
            return 0;
        }
        else
        {
            double b = B - A;
            double tangens = Math.Tan(1 / b);

            double a = tangens * (value - A);

            return Math.Round(a, 2);
        }
    }

    else
    {
        if (value >= B && value <= C)
        {
            return 1;
        }
        if (value <= A || value >= D)
        {
            return 0;
        }
        if (value > A && value < B)
        {
            double b = B - A;
            double tangens = Math.Tan(1 / b);

            double a = tangens * (value - A);

            return Math.Round(a, 2);
        }
        if (value > C && value < D)
        {
            double b = D - C;
            double tangens = Math.Tan(1 / b);

            double a = tangens * (D - value);

            return Math.Round(a, 2);
        }
        else
        {
            return 0;
        }
    }
}



它基本上计算y坐标在三角形在不同的方案。我用这来计算给定值多少融入一个模糊集。

It basically calculates y coordinate in triangle in different scenarios. I am using this to count how much the given value fits into a fuzzy set.

推荐答案

实体框架的其中,期待一个表达式树,这将被转换为T-SQL语句。不幸的是没有从你的 DoMath 法翻译T-SQL,所以你必须要拉下来,结果到内存中,而然后的通话其中,因为你拥有它。原因是,一旦你的结果是在内存中,LINQ方法标准的代表,而不是表达式树工作,所以有什么可以放在那里没有任何限制

Entity Framework's Where is expecting an Expression Tree, which will get converted to a t-sql statement. Unfortunately there is no translation from your DoMath method to t-sql, so you'd have to pull the results down into memory, and then call Where as you have it. The reason is that once your results are in memory, LINQ methods work on standard delegates, not expression trees, so there are no restrictions on what can be put in there

要做到这一点,只是钉了 AsEnumerable()在前面的其中, - 当然,这将拉低你的整个表到内存中,因此,只有做到这一点,如果它是相当小的。

To do this, just tack an AsEnumerable() in front of the Where—of course that will pull down your whole table into memory, so do this only if it's reasonably small

var filtered = db.Lorems.AsEnumerable().Where(x => x.DoMath(someValue) > 0);



当然,如果你能识别一些基本的情况下,你的 DoMath 将的的大于0,则可以过滤这些结果出来了前面,使用表达式树。这将修剪下来来自数据库的结果。你可以的然后的内存过滤休息。我不知道你的 DoMath 方法做什么(你暗示它更复杂,比你的问题的上市),但假设这样的事情应该工作:

Of course if you can identify some basic circumstances under which your DoMath will not be greater than 0, you can filter those results out up front, using an expression tree. This will trim down the results coming from the database. You can then filter the rest in memory. I have no idea what your DoMath method does (you imply it's more complicated than what's listed in your question), but hypothetically something like this should work:

var filtered = db.Lorems
                 .Where(x => x.A < 10 && x.B != x.C)
                 .AsEnumerable() 
                 .Where(x => x.DoMath(someValue) > 0);

这篇关于使用实体法的结果与lambda表达式或LINQ查询过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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