1

I have extension method that has the following signature:

public static class GenericSeeder
{
    public static void Seed<TSeed, TEntity>(this DbContext context, IEnumerable<TSeed> seeds, Expression<Func<TEntity, TSeed, bool>> predicate)
    {
        // code that I'm looking for goes here
    }
}

To have better understanding of what the method is doing, here's how it should be used:

context.Seed<SeedClass, EntityClass>(seeds, (entity, seed) => entity.Name == seed.OtherProperty);

So basically, I am using the predicate to check whether the seed was already applied. However, in order to do a check, I must use Where or FirstOrDefault from Linq to Entities, which takes the following as parameter:

Expression<Func<TEntity, bool>> predicate

So my lambda expression is function of 2 IN parameters (TSeed, TEntity) and 1 OUT parameter (bool). I need to iterate through provided collection of TSeed objects, and for each of those, use that object as parameter for my lambda expression, to generate LINQ 2 Entities lambda expression which has 1 IN parameter (TEntity) and 1 OUT parameter (bool).

Is there a way to do a partial invoke of lambda expression / func to get another lambda expression / func?

2
  • Although I think I know what you are asking for I am not 100% sure. You can do multi-statement lambdas using something like (x,y)=>{x.Frob();return y==x;} or something like that (compiled fine in my head, may not with a real compiler). This would allow you to internally do stuff with only one property and then the other. I can't guarantee that any linq provider apart from linq to objects will be able to do things with that (and indeed would expect probably not).
    – Chris
    Jun 23, 2014 at 13:37
  • The comparison in the predicate will always be == ? Jun 23, 2014 at 14:07

3 Answers 3

1

Through the use of LINQKit to allow expressions to be invoked in such a way that they will be transformed into other expressions the implementation of your method becomes fairly trivial:

public static IQueryable<TEntity> Seed<TSeed, TEntity>(
    this DbContext context,
    IEnumerable<TSeed> seeds,
    Expression<Func<TEntity, TSeed, bool>> predicate)
{
    return context.Set<TEntity>()
            .AsExpandable()
            .Where(entity => seeds.Any(seed => predicate.Invoke(entity, seed)));
}
2
  • Sorry, but although your answer has a value (thus upvote) for specific scenarios, for my scenario, due to the rest of implementation it is not more optimal. You may be saving time on expression to T-SQL conversion, but on the other hand I would have again to iterate through results of your query and find matching entity for each of the seeds (since you are grabbing everything all at once with query growing potentially insanely large if too many seeds are used). Jun 23, 2014 at 17:38
  • @BarisaPuter You can easily group the entities by the seed value if your method accepts the appropriate projection as a parameter, or simply by expecting the caller to group the seeds given the output. The effort spent grouping the values either way is not particularly large. If you have a lot of seeds, then doing a lot of DB round trips to fetch them all is a big problem. Each of those round trips will be very expensive.
    – Servy
    Jun 23, 2014 at 17:42
0

I have no idea what you are doing, but this is how you do partial application in c#:

Func<int,bool, string> twoInFunc= (int a, bool b) => a.ToString() + b.ToString();

int number = 7;

Func<bool, string> oneInFunc= (bool b) => twoInFunc(number,b);
1
  • Unfortunately your solution isn't helping me, since I require Expression transformation due to requirements by Linq 2 Entities. If I try what you suggest, I end up with: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities. Jun 23, 2014 at 16:12
0

I've managed to create solution on my own. I did however use the infamous LinqKit extension library and it's AsExpandable() extension method.

LinqKit can be found here: NuGet link

So this is the implementation that works with Linq 2 Entities:

public static void Seed<TSeed, TEntity>(this DbContext context, IEnumerable<TSeed> seeds, Expression<Func<TEntity, TSeed, bool>> predicate)
            where TEntity : class
            where TSeed : class
        {
            foreach (TSeed seed in seeds)
            {
                Expression<Func<TEntity, bool>> matchExpression = (entity) => predicate.Invoke(entity, seed);
                TEntity existing = context.Set<TEntity>().AsExpandable().FirstOrDefault(matchExpression);

            // Rest of code is omitted as it is not related to the original question.
            // The query above is properly executed by Linq 2 Entities.
            }
        }
4
  • This is evaluating the query to a value for every single seed. You shouldn't be doing that.
    – Servy
    Jun 23, 2014 at 16:59
  • @Servy Can you provide better solution? Jun 23, 2014 at 17:03
  • Yes, I can provide a better solution.
    – Servy
    Jun 23, 2014 at 17:03
  • @Servy Would you mind? :) Although I'm not worried about performance, as this is method that will be invoked only when migrations are running, I am indeed interested in viewing better approach than this :) Jun 23, 2014 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.