Using Where-Clause with predicate causes Npgsql.PostgresException
See original GitHub issueI’m using linq2db with PostgreSQL 9.6 Target framework 4.5.2 linq2db 1.8.0 npgsql 3.2.2
I got an Npgsql.PostgresException when executing a Select statement using Where-Clause passing a predicate. v1.7.6 works without any errors. Also v1.8.0 works when passing a lambda. Working Sample:
var data = objDB.t_trade_status.Where(y=>y.trade_status_id >= 1 && y.trade_status_id <= 4).ToList();
Exception Sample:
System.Linq.Expressions.Expression<Func<t_trade_status, bool>> predicate = PredicateBuilder.True<t_trade_status>();
predicate= predicate.And(data => data.trade_status_id >= 1);
predicate= predicate.And(data => data.trade_status_id <= 4);
var data = objDB.t_trade_status.Where(predicateOuter).ToList(); -> raised PostgresException (see stacktrace below)
PredicateBuilder is a helper class from http://www.albahari.com/nutshell/predicatebuilder.aspx The database table t_trade_status looks like this:
CREATE TABLE public.t_trade_status
(
trade_status_id integer NOT NULL DEFAULT nextval('t_trade_status_trade_status_id_seq'::regclass),
trade_status_name character varying(20) NOT NULL,
CONSTRAINT trade_status_id_pk PRIMARY KEY (trade_status_id)
)
Stacktrace:
at Npgsql.NpgsqlConnector.<DoReadMessage>d__147.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
at Npgsql.NpgsqlConnector.<ReadMessage>d__146.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
at Npgsql.NpgsqlConnector.<ReadExpecting>d__153`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
at Npgsql.NpgsqlDataReader.<NextResult>d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.<Execute>d__71.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
at Npgsql.NpgsqlCommand.<ExecuteDbDataReader>d__92.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at LinqToDB.Data.DataConnection.ExecuteReader(CommandBehavior commandBehavior)
at LinqToDB.Data.DataConnection.LinqToDB.IDataContext.ExecuteReader(Object query)
at LinqToDB.Linq.Query`1.<RunQuery>d__11.MoveNext()
at LinqToDB.Linq.Query`1.<Map>d__69.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at WL.DB.Test.PostgresCRUD.Logging_ReadUserTableViaPredicate() in PostgresCRUD.cs:line 31
And another note: In the exception I see the generated SQL Statement that looks like it’s invalid:
SELECT
t1.trade_status_id,
t1.trade_status_name
FROM
"public".t_trade_status t1
WHERE
(()) AND t1.trade_status_id >= 1 AND t1.trade_status_id <= 4
Could you please share you thougths on this issue?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Query with large WHERE clause causes timeout exception ...
In my test environment, SqlServer (LocalDB) simply refuses to execute the generated query with the reason as being too complex. PostgreSQL ...
Read more >Using column alias in a WHERE clause doesn't work
The (historic) reason behind this is the sequence of events in a SELECT query. WHERE and HAVING are resolved before column aliases are ......
Read more >Documentation: 15: 11.8. Partial Indexes
A partial index is an index built over a subset of a table; the subset is defined by a conditional expression (called the...
Read more >PostgreSQL do different equality predicates make a ...
Recently I wondered whether the usage of different equality predicates ('=' and 'in') in a query would differently costed, and thus could lead ......
Read more >Pattern Matching and Regular Expressions in PostgreSQL
As such, SQL offers a filter predicate “WHERE” using which users can filter the query and select the results that only match the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sdanyliv may be, please check.
@Kempe also please try
LinqToDB.Common.Configuration.Linq.UseBinaryAggregateExpression = false
Cool. Thanks a lot for your help. That was really fast!