Extract expression from BlToolkit LINQ so it can be compiled

237 Views Asked by At

Im trying to extract this expression:

t => t.DayEnd == s.DayEnd && t.DayStart == s.DayStart

into extern variable called expression.

And I want it to extract it in a way so i can use this variable in the next BlToolkit LINQ query.

private void InsertOrUpdate(IQueryable<CccPricingPricedDays> source, Table<CccPricingPricedDays> target)
    {
        Expression<Func<CccPricingPricedDays,CccPricingPricedDays, bool>> expression = (s,t) => t.DayEnd == s.DayEnd && t.DayStart == s.DayStart;

        //doplneni chybejicich
        source.Where(s => !target.Any(t => t.DayEnd == s.DayEnd && t.DayStart == s.DayStart))
              .Insert(target, table => table);
    }

I can find a way how to insert the variable so it can be compiled.

1

There are 1 best solutions below

0
McX On

If you want to reuse expressions with Linq, you may want to have a look at LinqKit. It walks inside your expression and replaces all the function calls by their contents before the sql conversion.

For example :

private void InsertOrUpdate(IQueryable<CccPricingPricedDays> source, Table<CccPricingPricedDays> target)
{
    Expression<Func<CccPricingPricedDays,CccPricingPricedDays, bool>> expression = (s,t) => t.DayEnd == s.DayEnd && t.DayStart == s.DayStart;

    source
          .AsExpandable()
          .Where(s => !target.Any(t => expression(s, t)))
          .Insert(target, table => table);
}