Handling null values in where clause using LINQ-to-SQL - c#

The LINQ-to-SQL query in Visual Studio generates an SQL query with errors. In LINQPad, the same LINQ query using the same database (or DataContext) runs just fine.
LINQ Query
var accesDomaines = from t in db.Access
where t.IdUser == access.IdUtilisateur
where t.IdDomain != null
where t.IdRole == access.IdRole
where t.IdPlace == access.IdPlace
select t;
Here's a small part of generated SQL where the error occurs:
WHERE (...) AND ([t3].[IdRole] = ) AND (...)
After the equals in where clause, there's literally nothing ! In the SQL query of LINQPad we see the good where clause:
WHERE (...) AND ([t3].[IdRole] IS NULL) AND (...)
When I compare the two generated SQL queries from VS and LINQPad, line by line, this is the same thing. Except LINQPad is using params and also the missing right part of equal in where clause of Visual Studio, as shown before.
Note 1
In the LINQ query, I tried with this syntax in where clauses:
where t.IdRole.Equals(acces.IdRole.Value)
But also generates a bad result. I even tried something like this before the LINQ query:
if (!acces.IdRole.HasValue) { acces.IdRole = null; }
Note 2
Properties are nullable integers. I do want null in query if property is null. Obviously, I want the value of property if there's a value.
Note 3
I have tried the proposition made in this question: Linq where column == (null reference) not the same as column == null
...with no success.
Any explanation of two similar LINQ queries, but generating a good and a bad SQL query? Any suggestion to solve this problem?
Thank you!

try this:
where object.Equals(t.IdRole, access.IdRole)

Use
object.Equals()
.Net would take care of generating correct sql for null condition.
Example:
Say you have a street table with columns like Suffix and Prefix. Then following linq query doesn't work:
string suffix = "ST";
string prefix = null;
var map = from s in Streets
where s.Suffix==suffix || s.Prefix==prefix
select s;
It would generate following sql:
SELECT [t0].[StreetId], [t0].[Prefix], [t0].[Suffix]
FROM [Street] AS [t0]
WHERE ([t0].[Suffix] = #p0) AND ([t0].[Prefix] = #p1)
we can clearly see it will not return any result.
Using object.Equals():
string suffix = "ST";
string prefix = null;
var map = from s in Streets
where object.Equals(s.Suffix, suffix) && object.Equals(s.Prefix,prefix)
select s;
would generate sql:
SELECT [t0].[StreetId], [t0].[Prefix], [t0].[Suffix]
FROM [Street] AS [t0]
WHERE ([t0].[Suffix] IS NOT NULL) AND ([t0].[Suffix] = #p0)
AND ([t0].[Prefix] IS NULL)
Which is correct one.
(Little late, but wanted to expand the answer for the benefit of others)

Have you tried verifying whether your properties had values with the HasValues property provided by the Nullables?
where t.IdRole == access.IdRole.HasValues ? access.IdRole.Value : null
Perhaps this could work. I haven't used LINQ-to-SQL really.

This example worked for me:
var aff3 = from a in context.Affiliates
where ((name == null && a.CompanyName == null) || (a.CompanyName == name))
select a.ID;

Related

PostgreSQL, Linq C# error 'could not determine data type of parameter $1'

I am getting this error "could not determine data type of parameter $1"
and my where clause where I am getting the error is like the following:
var result = from Table in model.Table
where (filter.XId.HasValue ? Table.XId == filter.XId: true)
select new TableEntity
{
ID = Table.XId
};
If my code was only like this 'Table.X == filter.X', it works ...
How can I fix this?
and I am getting this problem only with PostgreSQL database ....
First about what that error usually means. When making parametrized queries to PostgreSQL, all parameters should be referenced in query itself. When you add more parameters than used in query, usually the error above appears.
It seems that when whatever EF provider for PosgreSQL you use converted your statement to SQL, it created more parameters than needed.
In general, it might be hard for EF providers to analyze and correctly parse statements like you used, so good practice is to use statements that are "closer" to SQL in certain sense. In your case equivalent query which is "closer" to SQL would be:
where (filter.XId == null || Table.XId == filter.XId)
If you want to generate different queries based on the value of filter, you can do something like this:
var query = (IQueryable<Table>) model.Table;
if (filter.XId != null) {
query = query.Where(row => row.XId == filter.XId);
}
var result = query.Select(row => new TableEntity {
Id = row.XId
});
As it is the first SO answer in Google -
Marked answer didn't help me in similar case (EF6 + Postgre), so I had to use .AsEnumerable();
Yeah, it could be bad for perfomance but it fitted well in my case.
So this one would work:
var query = model.Table
.AsEnumerable()
.Where (p => filter.XId.HasValue ? p.XId == filter.XId: true);

Linq query translation to SQL messes up null parameter values

Here's a simple LINQ query:
var objs = db.Objects.Where(o => o.Field1 == val);
This translates to SQL query:
select * from [Object] where Field1 = #p1
Trouble is, the value of val can also legitimately be null. And SQL doesn't like comparing nulls; it insists on the syntax ... where Field1 is null.
Is there any way of doing this neatly, short of using a ?? / isnull operation?
This, again, is an EF weakness in LINQ support. Good old LINQ to SQL translated this properly depending on the runtime value of val.
I suggest you go with this:
var objs = db.Objects.Where(
o => (o.Field1 == val) || (o.Field1 == null && val == null));
If EF translates this litterally, the SQL Server query optimizer will actually pick up this pattern and optimize it to an "equals-with-nulls" check. You can even seek indexes using this code pattern, it just works. In the query plans this shows up as IS in contrast to EQ.
How about .HasValue?
var objs = db.Objects.Where(o => !o.Field1.HasValue && o.Field1 == val);

Linq To SQL select statement translates to Where 0 = 1

I have the following Linq to SQL query.
var users = rdc.Users.Where(u => u.EmailAddress.Equals(emailaddress) && password.Equals(hash));
But it always translates to:
SELECT [t0].[Id], [t0].[Guid], [t0].[RoleId], [t0].[EmailAddress], [t0].[Password], [t0].[FirstName], [t0].[LastName], [t0].[Created], [t0].[Activated], [t0].[LastAction]
FROM [dbo].[Users] AS [t0]
WHERE 0 = 1
I just re-imported the tables into my DBML file to insure that the latest schema is reflected, but it still removes my clause and adds WHERE 0 = 1
I can use ExecuteQuery<T>(string, params[]) but I want to find the root cause of this.
Are hash and password defined above this line? It looks like you might have forgetten to add the u. in your query. If password was already defined, then you wouldn't see an error, but it would never equal hash.
Did you mean to write:
var users = rdc.Users.Where(u => u.EmailAddress == emailaddress && u.Password == hash);
^^^
Also, #Mark pointed out that using the == operator will show errors that the .Equals operator might ignore.
For those, who also looking for solution, EF can set query filter on WHERE 0 = 1 in case if your subquery always returns empty result. Maybe this is some kind of optimisation.
So, look closely on your subquery. Some expression there always turns it to empty result.
Example:
return clientIds == null ? result : result.Where(r => ...);
Here, in subquery method return, if clientIds is List<T>, then expression will filter all results because list is always not null, even if it has no elements.
The correct way is like:
return clientIds?.Count < 1 ? result : result.Where(r => ...);

Null coalesce not working in LINQ query

Take this:
int? item1 = null;
int? item2 = null;
someObjectList.Where(x => x.SomeItem1 == (item1 ?? x.SomeItem1)
&& x.SomeItem2 == (item2 ?? x.SomeItem2)
);
Where someObjectList is not empty and SomeItem1 and SomeItem2 is null in all the objects in the list.
Why is it returning nothing?
EDIT:
My Code:
public void GetPlacementsByMaterial(long clientMaterialID)
{
ClientMaterial clientMaterial = ((ApplicationEntityModel)NavigationItem.ObjectContext).ClientMaterial.FirstOrDefault(x => x.ClientMaterialID == clientMaterialID);
var list = GetPlacementList(supplier, mediaSpace);
PlacementsList = list.Where(x => x.MediaCategoryFormatID == (clientMaterial.MediaCategoryFormatID ?? x.MediaCategoryFormatID)
&& x.MediaCategorySizeID == (clientMaterial.MediaCategorySizeID ?? x.MediaCategorySizeID)
);
}
All ID's are Nullable<long>.
EDIT:
SQL Profiler:
SELECT *
FROM [dbo].[CampaignSchedulePlacements] AS [Extent5]
WHERE ([Extent5].[MediaCategoryFormatID] = [Extent5].[MediaCategoryFormatID]) AND ([Extent5].[MediaCategorySizeID] = [Extent5].[MediaCategorySizeID])
Note: cleaned up the `SQL.
In SQL, NULL is not equal to NULL.
You can interpret NULL as meaning: "there is value, but I don't know what it is". So if you're comparing two NULL values, you're really asking "is the first unknown value equal to the second unknown value?" Of course, there is no reason to assume they are, so SQL will say "no".
I am assuming that this is causing your problem. You can verify that by looking at the actual SQL produced. If it's using the SQL = operator, this is indeed the problem. You can verify that by running the SQL in a database tool, such as SQL Management Studio in case you're using SQL Server.
UPDATE:
The condition
([Extent5].[MediaCategoryFormatID] = [Extent5].[MediaCategoryFormatID])
will indeed return false when [Extent5].[MediaCategoryFormatID] is NULL.
That answers the question "Why is it returning nothing?"
However, another question come to mind: why would the entity framework generate that SQL from this linq query?
I'm afraid that linq to entities is not exactly known for the quality of its SQL generation, and this case seems to confirm that. You might consider Linq to SQL. Even if that seems to be a dead-end track in the long run, the current implementation if a lot better than linq to entities.
In either case, have you tried something like
someObjectList.Where(x =>
!item1.hasValue ||
x.SomeItem1.HasValue && x.SomeItem1.Value == item1.Value)
Make sure to verify that under the profiler as well though, linq to entities might mess it up too.

Linq query works with null but not int? in where clause

I have a linq query function like (simplified):
public IList<Document> ListDocuments(int? parentID)
{
return (
from doc in dbContext.Documents
where doc.ParentID == parentID
select new Document
{
ID = doc.ID,
ParentID = doc.ParentID,
Name = doc.SomeOtherVar
}).ToList();
}
Now for some reason when I pass in null for the parentID (currently only have data with null parentIDs) and I get no results.
I copy and paste this query into LinqPad and run the following:
from doc in dbContext.Documents
where doc.ParentID == null
select doc
I get back a result set as expected...
The actually query has left join's and other joins but I have removed them and tested it and get the same result so the joins are not affecting anything. The app and LinqPad are both connected to the same DB as well.
Edit: Tested with just 'null' in the appliction query and it returns the result as expected so the issue is using null vs int?. I have updated question to make it more useful to others with the same issue to find this thread.
Literal null values are handled differently than parameters which could be null. When you explicitly test against null, the generated SQL will use the IS NULL operator, but when you're using a parameter it will use the standard = operator, meaning that no row will match because in SQL null isn't equal to anything. This is one of the more annoying .NET/SQL semantic mismatches in LINQ to SQL. To work around it, you can use a clause like:
where doc.ParentID == parentID || (doc.ParentID == null && parentID == null)
You can use also something like ...
from doc in dbContext.Documents
where doc.IsParentIDNull()
select doc
It worked for me!
hope it work for you!

Categories

Resources