Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Entity Framework expression to SQL translation

Look at this IQueryable:

var query = from e in db.EntityA
            select new MyViewModel 
                       {
                           field1: e.field1,
                           field2: e.field2,
                           field3: (e.field4 == 4 ? "four" : e.field4 == 3 : "three":....)
                       }

I want the field3 calculation to be made in SQL, because I may filter or order on this field.

At this step, it works fine but the code is ugly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is what I want to do:

public String SomeFunction(EntityA e)
{
    if (e.field4 == 4)
    {
       return "four";
    }
    ...
}

var query = from e in db.EntityA
            select new MyViewModel 
                       {
                           field1: e.field1,
                           field2: e.field2,
                           field3: SomeFunction(e)
                       }

It does not work because EF is not able to translate my function to a SQL expression.

My question is: how can I help EF to do that ?

I need to use this expression in many queries. So I have tried this:

public String SomeFunction(EntityA e)
{
    return (e.field4 == 4 ? "four" : e.field4 == 3 : "three":.... );
}

I don’t why it does not work because it is exactly the same expression!

Other question: is there a way for me to create EF "add-ons"?

Thanks a lot for your help

>Solution :

You cannot do that elegantly without third party extensions. I would propose LINQKit for such task

It needs just configuring DbContextOptions:

builder
    .UseSqlServer(connectionString)
    .WithExpressionExpanding(); // enabling LINQKit extension

Write helper function:

public static class MyExensions
{
    [Expandable(nameof(SomeFunctionImpl))]
    public static string SomeFunction(int value)
        => throw new InvalidOperationException();

    private static Expression<Func<int, string>> SomeFunctionImpl()
    {
        return value => 
            value == 4 ? "four" : 
            value == 3 ? "three" : .... ;
    }
}

And usage in LINQ Queries:

var query = 
    from e in db.EntityA
    select new MyViewModel 
    {
        field1 = e.field1,
        field2 = e.field2,
        field3 = MyExensions.SomeFunction(e.field4)
    };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading