DateTime clause being ignored in Linq to SQL - linq

I have the following LINQ query
db.Documents.Where(x => x.PublicationId == db.PublicationId &&
x.CancellationDate == null)
The resulting SQL is ignoring the CancellationDate clause and timing out as a result.
I have tried the same query using other date fields all with the same result.
Is there something fundamental I am missing to execute SQL with date clauses using LINQ to SQL?
Edit:
The resulting SQL is essentially
exec sp_executesql N'SELECT *
FROM [dbo].[Document] AS [t0]
WHERE [t0].[PublicationId] = #p0',N'#p0 uniqueidentifier',#p0='47892838-9B52-4A4C-B4A2-8302A3FF5F22'
The SQL type of CancellationDate is nullable DateTime and the DBML property is Nullable, Server Data Type DateTime and Type DateTime (System.DateTime)
Also, if I change the LINQ to x.CancellationDate < DateTime.Now the resulting SQL also does not include this clause. !x.CancellationDate.HasValue has the same effect

Related

Oracle not using index, Entity Framework & Devart DotConnect for oracle

The table in question has ~30mio records. Using Entity Framework I write a LINQ Query like this:
dbContext.MyTable.FirstOrDefault(t => t.Col3 == "BQJCRHHNABKAKU-KBQPJGBKSA-N");
Devart DotConnect for Oracle generates this:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE (Extent1.COL3 = :p__linq__0) OR ((Extent1.COL3 IS NULL) AND (:p__linq__0 IS NULL))
FETCH FIRST 1 ROWS ONLY
The query takes about four minutes, obviously a full table scan.
However, handcrafting this SQL:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE Extent1.COL3 = :p__linq__0
FETCH FIRST 1 ROWS ONLY
returns the expected match in 200ms.
Question: Why is it so? I would expect the query optimizer to note that the right part is false if the parameter is not null, so why doesn't the first query hit the index?
Please set UseCSharpNullComparisonBehavior=false explicitly:
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.QueryOptions.UseCSharpNullComparisonBehavior = false;
If this doesn't help, send us a small test project with the corresponding DDL script so that we can investigate the issue.

Parametrizing a sub query with jdbc PreparedStatement

I have the following query where the first argument itself is a subquery
The java code is:
String query = select * from (?) where ROWNUM < ?
PreparedStatement statement = conn.preparedStatement(query)
statement.setString(1, "select * from foo_table")
statement.setInt(2, 3)
When I run the java code, I get an exception. What alternatives do I have for making the first subquery statement.setString(1, "select * from foo_table") a parameter?
This is not possible, parameter placeholders can only represent values, not object names (like table names, column names, etc) nor subselects or other query elements.
You will need to dynamically create the query to execute using string concatenation, or other string formatting/templating options.

using Oracle sysdate with Nhibernate Linq

When creating a Linq expression in Nhibernate 3.2, is there way to pass the sysdate as part of the generated sql?
For example, something like this
from c in _session.Query<Question>
where Question.EDate.Date = sysdate
or
from c in _session.Query<Question>
where Question.Edate.Date == trunc(sysdate)
to generate a select statement like:
select * from question where trunc(EDate) == trunc(sysdate)
Only by extending the LINQ provider (not that difficult):
http://fabiomaulo.blogspot.pt/2010/07/nhibernate-linq-provider-extension.html
http://www.primordialcode.com/blog/post/nhibernate-3-extending-linq-provider-fix-notsupportedexception

how to write conditional query from two tables using LINQ

There are two tables in two database
Promotion: code, source, broker
BrokerNo: Id, Name, BrokerNo
What I want to query are all BrokerNo.Name records satisfying the condition of Promotion.broker=BrokerNo.BrokerNo and Promotion.source="asdf"
How to write the query statement using LINQ?
from promotion in Promotion
from brokerNo in BrokerNo
where promotion.source == "asdf" && promotion.broker == brokerNo.BrokerNo
select brokerNo.Name

Passing java.sql.Date to sql query

This is a part of my code
java.sql.Date d1=java.sql.Date.valueOf("2011-03-02");
java.sql.Date d2=java.sql.Date.valueOf("2011-03-10");
java.sql.Date systemDate=java.sql.Date.valueOf("2011-03-04");
String sql="select id from period where '"+systemDate+"' between '"+d1+"' and '"+d2+"'";
This is my code. I want to get the id which falls between these dates. But i am not getting the desired result. I am getting back all the id present in the table.
Do you have good results when you use other SQL tools (like pgadmin for PostgreSQL or SQL Developer for Oracle)?
Is so then try to use PreparedStatement. In your case this will look like:
PreparedStatement pstmt = con.prepareStatement("select id from period where ? between ? and ?");
pstmt.setDate(1, systemDate);
pstmt.setDate(2, d1);
pstmt.setDate(3, d2);
You're not using a field from your table to compare with your date range.
What you're doing is checking if 04-03-2011 is between 02-03-2011 and 10-03-2011, which is true for every record.
What you want to do is something like this (assuming start_date is a column in your table):
String sql="select id from period where start_date between '"+d1+"' and '"+d2+"'";
(Also, I agree with the answer above that prepared statements are a better way to construct queries.)

Resources