Petr Babička - 311 - Feb 21, 2014

Hi, passed this test? 
https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/SqlServerExpressionsTest/Program.cs

because this:
var q = db.SqlExpression<Poco>().Select(r => new { … }).Where(r => (…) && (…));

for (int i = 0; i < 10; ++i) 
{
q.Where(r => r.Count > i);
db.Select(q);
}

generate strange sql:
… WHERE (Count > 0) 
… WHERE ((Count > 0) AND (Count > 1))
… WHERE (((Count > 0) AND (Count > 1)) AND (Count > 2))

instead of:
… WHERE (Count > 0) 
… WHERE (Count > 1) 
… WHERE (Count > 2) 
 I know, with And it’s same, but exists some option to have base query and changing only part of it?

Note sure what the question is but the behavior you’re seeing is because the Sql expression builder mutates when you add new expressions on it.

You would have to apply your mutation to a new instance to use part of the query.

Petr Babička:

In that test is:
SqlExpression<Author> ev = OrmLiteConfig.DialectProvider.SqlExpression<Author>();

// select authors from London, Berlin and Madrid : 6
ev.Where(rn => Sql.In(rn.City, new object[] { “London”, “Madrid”, “Berlin” }));
db.Select(ev);

// select authors from Bogota and Cartagena : 7
ev.Where(rn => Sql.In(rn.City, new object[] { “Bogota”, “Cartagena” }));
db.Select(ev);

but when i’m trying do that i get:
[OK] // select authors from London, Berlin 

[FAIL] // Madrid  and from Bogota and Cartagena
because a get:
// select authors from London, Berlin and from Madrid  and from Bogota and Cartagena

Right because of exactly what I was saying, the SQL Expression mutates.

You would have to create ot from 2 new expresions:
db.SqlExpression<Author>().Where(m => )

Although I could add a sqlExpr.Clone() method to copy the snapshot of the expression.

I’ve just added cloning support to SQL expressions which you can use like: https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/Expression/ExpressionChainingUseCase.cs#L192

Will be available in the next release.