Error variable 'x' of type 'myClass' referenced from scope '', but it is not defined

Viewed 3019

This code works fine:

ICriteria criteria = GetSession().CreateCriteria<MyClass>();
criteria.Add(Restrictions.Where<MyClass>(x => x.Field1 >= myVariable));

But the following code does not work:

criteria.Add(Restrictions        
        .Where<MyClass>(x =>          
        (x.Field1 +
        x.Field2 +
        x.Field3 +
        x.Field4) >= myVariable));

The above code get this error in execution:

Variable 'x' of type 'myClass' referenced from scope '', but it is not defined

Help please (sorry for my bad English).

Sara

Edit 1

My Temporary solution is:

var result = criteria.List<MyClass>();    
result.Where(x => (x.Field1 + x.Field2 + x.Field3 + x.Field4 >= myVariable));

and this work. I would prefer put the Where clause before selection...

Edit 2

The final solution is (as suggested from @mhoff):

var result = criteria.List<MyClass>();    
result.Where(x => this.GetSum(x) >= myVariable);

... do something ...

... ToList()


private int GetSum(MyClass x) {
 return (x.Field1 + x.Field2 + x.Field3 + x.Field4);
}
2 Answers
Related