none
TakeWhile Sum of property fulfills a condition RRS feed

  • Question

  • Hi,

    I'm looking at this SO answer, which implements this feature:

    public static IEnumerable<TSource> TakeWhileAggregate<TSource, TAccumulate>(
        this IEnumerable<TSource> source,
        TAccumulate seed,
        Func<TAccumulate, TSource, TAccumulate> func,
        Func<TAccumulate, bool> predicate)
    {
        TAccumulate accumulator = seed;
        foreach (TSource item in source) {
            accumulator = func(accumulator, item);
            if (predicate(accumulator)) {
                yield return item;
            }
            else {
                yield break;
            }
        }
    }

    However, I want this to be executed by the server.

    Is there a way to achieve that (I believe via Expression Trees)?
    I tried looking up the source code of the original TakeWhile in the EF sources to take it from there, but couldn't find it.

    Any suggestions?

    P.S. I'm using EF6.


    Shimmy

    Wednesday, October 14, 2015 4:50 AM

Answers

  • LINQ will try to transform the query to server-side queries if it can (the expression tree is the INPUT to this mechanism), otherwise it will be implemented on the client.  It's not too hard to make your query too complex for translation to server-side representation. If the query winds up executing on the client, then you can either manually try to game the provider by altering your query to an alternate form that you hope will be executed server side, or write your own LINQ provider (NOT EASY!) but here you can parse expression trees and construct and issue the server side queries yourself.  [Yeah...not for those without serious devotion to the craft.]

    Halfway between those two options is PredicateBuilder and might be worth investigating.


    Thursday, October 15, 2015 12:49 AM