3

I'm trying to implement a 'IN' clause in my EF query through a lambda expression as follows:

        /*lambda expression to evaluate: status in[a, b, c...n]*/
        _IN myIN = (allStatus, status) =>
        {
            bool lambdaResult = false;
            foreach (int s in statuses)
            {
                if (status == s)
                {
                    lambdaResult = true;
                    break;
                }
            }
            return lambdaResult;
        };

        result = (from v in _entity.CAVouchers
                  join vs in _entity.CAVoucherStatus.Where(vs => (myIN(statuses, vs.VoucherStatusId) == true)) on v.VoucherStatusId equals vs.VoucherStatusId
                  join vsr in _entity.CAVoucherStatusReason on v.VoucherStatusReasonId equals vsr.VoucherStatusReasonid
                  where v.CyberAgent.CAID == caid                       
                  select new ACPVoucher() 
                  {
                      ...
                  }).ToList();

This is throwing a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" error due to the "where" condition. Basically what I need is a "where statusId in [1, 2, 3]" kind of clause.

What is failing?...any tips on how to do this? The same approach is working for me when applied to a generic list.

Thanks

allendehl
  • 129
  • 3
  • 11

3 Answers3

3

You could just do a contains query:

where statuses.Contains(vs.VoucherStatusId)
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

Can you try this?

join vs in _entity.CAVoucherStatus.Where(vs => statuses.Contains(vs.VoucherStatusId)) on v.VoucherStatusId equals vs.VoucherStatusId
Bala R
  • 107,317
  • 23
  • 199
  • 210
0

Different LINQ providers support different tricks; as it happens LINQ-to-SQL works with invoke. EF: not so much. If you are building the expression by hand, you may need to either form the expression without invoke, or to use a re-writer to flatten it. There's a rewriter example here that illustrates how to do this: Combining two lambda expressions in c#

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900