Linq to SQL Writes Translatable Functions

I am sure that the answer to this question is: β€œYou cannot do this” or β€œNo, no, you misunderstood ...”, but:

I have a linq class, a thing that will happily accomplish this:

 var thingsWithAandB = from t in db.things
                       where t.propA.HasValue && t.propB.HasValue
                       select t;

But I do it a lot, and therefore I want:

partial class thing
{
    public bool hasAandB
    {
        get
        {
            return propA.HasValue && propB.HasValue;
        }
    }
}

and then:

var thingsWithAandB = from t in db.things where t.hasAandB select t;

But, of course, when I do this, I get the error "Can't translate this to SQL." And I understand that this is because calling methods in the middle of SQL queries is not possible, since my code and database are separate.

How to do it? It's impossible?

+3
source share
2 answers

That's quite possible. Damien Guard (damieng.com) has a sample on his blog showing how to do this.

http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider

+4

:

public static class MyExtensions{
    public static bool HasAandB(this thing t){
        return t.propA.HasValue && t.propB.HasValue;
    }
}

:

var thingsWithAandB = from t in db.things
                   where t.HasAandB()
                   select t;


, , , ...

-1

Source: https://habr.com/ru/post/1732046/


All Articles