0

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.

John Smith
  • 1,783
  • 6
  • 22
  • 36
  • 1
    How do you plan to use the expression? You can't use it as `IQueryable.Where` predicate. – Hamlet Hakobyan Apr 03 '14 at 14:44
  • can you please show me exactly how to do it? Im not sure how to extract this.. my goal is to send the predicate to the method as parameter so it can be used multiple times in different parts of code – John Smith Apr 04 '14 at 06:44

1 Answers1

0

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);
}
McX
  • 1,296
  • 2
  • 12
  • 16