0

I have searched a lot regarding this issue what is wrong here

Shorter version of my code is
/// companyid  is integer type value here is 220
    var cond1 = BuildExpression(companyId);
    var acntlst=entities.Accounts.Where(cond).ToList();

Account is a class querying against Account collection

BuildExpression function

 private static Expression<Func<Account, bool>> BuildExpression(string companyid)

 {
    var paramexp = Expression.Parameter(typeof (Account), "p");
     var proprty = typeof(Account).GetProperty("CompanyId");
     var prpexp = Expression.Property(paramexp, proprty);

     var varexp = Expression.Variable(typeof(Int32), companyid);
     var cond1 = Expression.Equal(prpexp, varexp);

     return Expression.Lambda<Func<Account, bool>>(cond1,paramexp);
    }

Error message is

The parameter '220' was not bound in the specified LINQ to Entities query expression

1 Answer 1

3
Expression.Variable(typeof(Int32), companyid);

This creates a variable named "220".
You never declared this variable or assigned it a value.

Instead, you want Expression.Constant, which takes a value and returns an expression which has that value.
(you will probably need to parse the string into an int)

However, you don't need to build this by hand at all.

Instead, you should just write return a => a.CompanyId == companyId

Sign up to request clarification or add additional context in comments.

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.