question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Where-Clause with predicate causes Npgsql.PostgresException

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ilicommented, May 19, 2017

@sdanyliv may be, please check.

@Kempe also please try LinqToDB.Common.Configuration.Linq.UseBinaryAggregateExpression = false

0reactions
Kempecommented, May 19, 2017

Cool. Thanks a lot for your help. That was really fast!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found