5

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);
}
sara
  • 124
  • 2
  • 11
  • Looks like this isn't the `Where` method form Linq. Is it something you've written or from a library you're using? It looks like it's doing something funky to the expression you're passing it. – jonnystoten Aug 16 '12 at 13:08
  • @applechewer: only GetSession() is method written by me. Everything else are NHibernate methods. – sara Aug 16 '12 at 13:16
  • this has nothing to do with multiline? http://stackoverflow.com/questions/5653703/can-a-c-sharp-lambda-function-be-more-than-one-line should not, just a guess. you could try adding {} to the lambda just to make sure. – Andreas Reiff Aug 16 '12 at 13:28
  • 1
    possible duplicate of http://stackoverflow.com/questions/4243890/nhibernate-restriction-with-sum-of-2-columns – nemesv Aug 16 '12 at 13:34
  • @AndreasReiff: I get the same error even when it is in line – sara Aug 16 '12 at 13:46
  • @nemesv: with code criteria.Add(Expression.Sql("Column1 > (Column2 + Column3)")); works, but I don't want use the name of columns. – sara Aug 16 '12 at 13:53
  • @sara: The other solution for that page might work, basically manually building the expression tree. – Guvante Sep 13 '12 at 21:31
  • How is Restrictions.Where defined in terms of typing? – Pharap Jan 06 '14 at 10:22
  • My understanding is that by doing .List you are evaluating at this point and the .Where is now regular LINQ. This is not what I was expecting. I want SQL, not LINQ, to handle it in the evaluation of the Criteria. What is the point of Restrictions.Where<>() if you can't seemingly use it in any possible way? – user99999991 Nov 02 '15 at 23:07

2 Answers2

0

If Restictions is an IEnumerable of MyClass then try to divide add and select:

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

criteria.Add(v);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Xilmiki
  • 1,453
  • 15
  • 22
0

How about turning your calculation into a property (this also has the benefit of encapsulating MyClass specifics):

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

criteria.Add(Restrictions.Where<MyClass>(x => x.GetSum > myVariable));
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
mhoff
  • 403
  • 4
  • 11
  • `criteria.Add(Restrictions.Where(x => this.GetSum(x) > myVariable)); private int GetSum(MyClass x) { return (x.Field1 + x.Field2 + x.Field3 + x.Field4); }` I have the same error :-( But is good idea move the sum (also because I reuse it) – sara Sep 14 '12 at 07:01
  • I create a method because I have to pass a parameter (MyClass) and then put `x.Field1`+ `x.Field2` + etc – sara Sep 14 '12 at 12:37
  • Why not simply `x => x.GetSum` - no need to pass "x" to "this.GetSum" – mhoff Sep 14 '12 at 13:04
  • But `GetSum` not knows `Field1`, `Field2`, etc that are property of MyClass – sara Sep 14 '12 at 13:42
  • `GetSum` resides in class `MyClass`, right? `Field1/2..` are all properties on `MyClass`? Please try it out in our environment. – mhoff Sep 14 '12 at 14:02
  • Sorry, `GetSum` isn't in MyClass because `MyClass` is an autogenerated mapping class and I don't want to touch. – sara Sep 14 '12 at 14:16
  • That... really isn't okay. What if you have a parameter? Can't be a property. So Restrictions.Where(x => x.Foo(param))) isn't possible? Why? That's makes no sense. – user99999991 Nov 02 '15 at 23:05
  • @user999999928 If you have a parameter other than `x` needed in the `GetSum` calculation, then you are right, `GetSum` can't be a property. But, please look at the original example which states that the Fields all resides in `MyClass`. @sara later stated that the class was autogenerated, and as such didn't leave room for the addition of `GetSum` - in this case `GetSum` could be formulated as an `MyClass` extension method in a static utility class. – mhoff Apr 06 '16 at 06:01