Using a Function in an Entity Framework Query's Select Clause

.net c# entity-framework linq

Question

I want to use Entity Frameworks to accomplish the following logic.

var items = from item in myContext
            select new {
                Value1 = TweakValue(item.Value1),
                Value2 = TweakValue(item.Value2)
            };

protected int TweakValue(int value)
{
    // Custom processing here
    return value;
}

Due to the call to action, this won'tTweakValue() in theselect clause. I am aware that the query gets translated into SQL, and the issue is thatTweakValue() not convertable to SQL. What is the most practical method to put this into practice? is my query. Do the values need to be converted using a second loop?

I'm still working on becoming used to LINQ expressions.

1
9
6/21/2011 6:37:46 PM

Accepted Answer

The easiest method would likely be to simply "transfer" the execution of the transformation to the client. You would only employ:

var items = myContext.Select(item => new { item.Value1, item.Value2 })
                     .AsEnumerable()
                     .Select(item => new {
                                 Value1 = TweakValue(item.Value1),
                                 Value2 = TweakValue(item.Value2)
                             });

Keep in mind that you have not to duplicate the names forValue1 and Value2 - just because it is the simplest.

If you're really interested in using query expressions:

var query = from item in myContext
            select new { item.Value1, item.Value2 };

var items = from item in query.AsEnumerable()
            select new {
                Value1 = TweakValue(item.Value1),
                Value2 = TweakValue(item.Value2)
            };

Put the filtering, sorting, etc. before the call to make that occur in the database if you wish to do filtering first.AsEnumerable() . For instance:

var query = from item in myContext
            where item.Foo == bar
            orderby item.Something
            select new { item.Value1, item.Value2 };

var items = from item in query.AsEnumerable()
            select new {
                Value1 = TweakValue(item.Value1),
                Value2 = TweakValue(item.Value2)
            };
12
6/21/2011 6:36:40 PM

Popular Answer

You just need another projection; no loop is necessary.

var items =  myContext.Select(i => new {
                 Value1 = item.Value1,
                 Value2 = item.Value2
             })
             .AsEnumerable()
             .Select(i => new {
                 Value1 = TweakValue(item.Value1),
                 Value2 = TweakValue(item.Value2)
              });

Revision: Depending onTweakValue does, you may upload the whole file to the server. A riff on your most recent illustration

public Expression<Func<Item, ItemProjection>> TweakValue()
{
    return item => new ItemProjection 
                   {
                       Value1 = item.Value1,
                       Value2 = item.Value2 + 0 // or something else L2E can understand...
                   }; 
}

Use it now as follows:

var exp = TweakValue();
var items =  myContext.Select(exp);

Note I'm keepingexp to prevent L2E from attempting to directly invokeTweakValue the query, which would not succeed.

Obviously, this only works ifTweakValue perform things that L2E is capable of.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow