Error of variable 'x' of type 'myClass' referring to scope '', but it is not defined

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 gets this error on execution:

A variable 'x' of type 'myClass' referencing the scope '', but this is not defined

Please help (sorry for my poor English).

Sarah

Change 1

My workaround:

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

and this work. I would rather put the Where clause before the choice ...

Edit 2

Final solution (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);
}
+5
source share
2 answers

If Restictionsis IEnumerableof MyClass, try splitting add and select:

var v = Restrictions
    .Where(x =>(x.Field1 + x.Field2 +  x.Field3 + x.Field4) >= myVariable));

criteria.Add(v);
0
source

( MyClass):

public class MyClass
{
    public int GetSum
    {
        get { return Field1 + Field2 + Field3 + Field4; }
    }
}

criteria.Add(Restrictions.Where<MyClass>(x => x.GetSum > myVariable));
0

All Articles