Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL writing translatable functions

I'm pretty sure the answer to this is "you can't do that" or "No, no, you misunderstand...", but:

I have a linq class, thing, which will happily execute this:

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

But I do this a lot, and so 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 "Can't translate that into SQL" errors. And I understand that this is because calling methods in the middle of SQL queries isn't possible, since my code and the database are separate.

What's the way to do this? Is it impossible?

like image 420
Greg Avatar asked Sep 03 '25 08:09

Greg


1 Answers

It is perfectly possible. Damien Guard (damieng.com) has a sample on his blog showing how to do that.

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

like image 152
KristoferA Avatar answered Sep 04 '25 20:09

KristoferA