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

3 Answers 3

3

You could just do a contains query:

where statuses.Contains(vs.VoucherStatusId)
Sign up to request clarification or add additional context in comments.

Comments

1

Can you try this?

join vs in _entity.CAVoucherStatus.Where(vs => statuses.Contains(vs.VoucherStatusId)) on v.VoucherStatusId equals vs.VoucherStatusId

Comments

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#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.