I have a problem with List of objects, which one have field for example Metadata, which is null. So when i use a.Metadata.ToLower().Contains(someText)
It shows me error, about null value. How Can I fix it ?
P.S. I see this problem first time, I tried to do the same with other List of objects which one also have null fields and it works, where can be problem ?
instead of
.Where(a => a.Metadata.ToLower().Contains(someText))
do
.Where(a => a.Metadata != null && a.Metadata.ToLower().Contains(someText))
Linq don't like null value but sometime we can't like exemple above just ignore what is null so i took habitude to cast null into the empty or minimal value.
so the value even null are cared like all other value .
have you tried a null fusion opérator in your linq query?
its : ?? operator
it will return rigth part if your data is null (in my case i return a string empty).
exemple i want sort a machine list that have some null string.
ListControleMachine.Sort((a1, a2) => (a1.MachineName ?? string.Empty).CompareTo(a2.MachineName ?? string.Empty));
you see when i sort my machine if its name is null i transform null into String.empty
Related
Can anyone explain why this apoc procedure returns null properties on the yielded value?? In this example the predicate is false.
MATCH (p:Posts)
CALL apoc.when(
p.region_audience IS NOT NULL AND NOT (2 IN p.region_audience),
"RETURN null",
"RETURN p AS post",
{p:p})
YIELD value
RETURN value,p.title;
So while value which should be equal to p is returned completely if you try to return value.title you get null. If your return p.title you do get the title. I know it has something to do with how the yield is computed.
Any help would be appreciated!
According to your condition p.region_audience IS NOT NULL AND NOT (2 IN p.region_audience), in apoc.when query:
In the ifQuery, the apoc.when returns RETURN null (value will be null) and in elseQuery returns p with Alias post.
So to access the result of the apoc.when query, you've to use the alias given in ifQuery or elseQuery : value.post.title.
Note that the result of value will always be null if the ifQuery is executed (when the condition is respected).
I am trying to make a fairly simple query with an optional parameter in Interbase. I am using Firedac in Delphi 10 Seattle to call it.
SELECT STATUS_ID FROM TABLENAME
WHERE
STATUS_ID=:STATUSID OR :STATUSID IS NULL
fails with Dynamic SQL Error Code = -804 Unknown Datatype.
I can isolate just the :STATUSID IS NULL part and it fails.
Setting the parameter to null with just STATUS_ID=:STATUSID works just fine so it is the :STATUSID IS NULL part that is throwing the error.
Just remembered:
some_field = :some_param or :some_param is null
can be replaced by
some_field = coalesce(:some_param, some_field)
Yet another solution to try.
The answer I came up with is to use zero as my 'all' clause.
WHERE
STATUS_ID=:STATUSID OR :STATUSID=0;
that works. Null is better because it is correctly asking for No Entity, but it seems the implementation in Firedac does not support it.
I know it's very late answer. But I had a similar problem.
I guess your problem is because you test STATUS_ID=:STATUSID before :STATUSID IS NULL. Maybe it'll work with (:STATUSID IS NULL) OR (STATUS_ID=:STATUSID)
For numeric fields, I came up with a slightly different solution. For integer PKeys, often you never have a value of 0. Meaning you can either pass NULL or 0 and this will still return all rows. Lil more flexible.
SELECT * FROM WHATEVER
WHERE
((:ID IS NULL) OR (:ID <= 0) OR (:ID = ID))
For string fields, either NULL or '' (empty string) will return all rows.
SELECT * FROM WHATEVER
WHERE
((:STR IS NULL) OR (TRIM(:STR) = CAST('' AS VARCHAR(10))) OR (:STR = CLIENT_NI))
You can even chain them up like
SELECT * FROM WHATEVER
WHERE
((:ID IS NULL) OR (:ID <= 0) OR (:ID = ID)) AND
((:STR IS NULL) OR (TRIM(:STR) = CAST('' AS VARCHAR(10))) OR (:STR = CLIENT_NI))
Probably a little less efficient than having a separate query for all scenarios, but if you're using an OK RDB, it'll optimize the parameter WHERE clause as static, and you won't lose much performance. I love this solution.
You could use a cast
SELECT STATUS_ID
FROM TABLENAME
WHERE STATUS_ID=:STATUSID OR cast( :STATUSID as integer ) IS NULL
This works in firebird, so I guess that it should work in Interbase as well.
I'm trying to use Contains() in a search procedure. The full text indexes are created and working. The issue arises because you cannot used Contains() call on a NULL variable or parameter, it throws an error.
This takes 9 sec to run (passing in non-null param):
--Solution I saw on another post
IF #FirstName is null OR #FirstName = '' SET #FirstName = '""'
...
Select * from [MyTable] m
Where
(#FirstName = '""' OR CONTAINS(m.[fname], #FirstName))
This runs instantly (passing in non-null param)
IF #FirstName is null OR #FirstName = '' SET #FirstName = '""'
...
Select * from [MyTable] m
Where
CONTAINS(m.[fname], #FirstName)
Just by adding that extra 'OR' in front of the 'contains' completely changed the Query Plan. I have also tried using a 'case' statement instead of 'OR' to no avail, I still get the slow query.
Has anyone solved this the problem of null parameters in full text searching or experience my issue? Any thoughts would help, thanks.
I'm using SQL Server 2012
You are checking value of bind variable in SQL. Even worse, you do it in OR with access predicate. I am not an expert on SQL Server, but it is generally a bad practice, and such predicates lead to full table scans.
If you really need to select all values from table when #FirstName is null then check it outside of SQL query.
IF #FirstName is null
<query-without-CONTAINS>
ELSE
<query-with-CONTAINS>
I believe, in the majority of times #FirstName is not null. This way you will access table using your full text index most of the time. Getting all the rows from table is a lost cause anyway.
From a logical standpoint, the first query takes longer to execute because it has to evaluate 2 conditions:
#FirstName = '""'
and ,in case the first condition fails, which should be the majority of the time,
CONTAINS(m.[fname], #FirstName)
My guess is that in your table, you don't have any null or empty FirstName, that's why the results are the same. Otherwise, you would have a few "" in the result set as FirstName.
Maybe you should try reversing the order to see if it makes any difference:
WHERE (CONTAINS(m.[fname], #FirstName) OR #FirstName = '""')
name <> myname
I feel a bit stupid asking this question, but I am using the predicate above in a subquery. I was expecting to see all rows where name was not equal to my name. However, rows that have a NULL value for name are not being returned. Is this correct behavior?
Yes, that is to be expected. See Null Values in the "Predicate Programming Guide":
A comparison predicate does not match any value with null except null
(nil) or the NSNull null value (that is, ($value == nil) returns YES
if $value is nil).
...
If you want to match null values, you must include a specific test in addition to other comparisons, ...
That means that both "name = myname" and "name <> myname" evaluate to NO if name
is NULL.
You can change your predicate to
name <> myname OR name = NULL
to cover both cases.
We are using $filter system query option in OData to execute filters where the filter value is sent in at runtime.
As an example the OData URL would be like this:
http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=CustomerID eq #InCustomerID and Country eq #InCountry
where #InCustomerID & #InCountry are the input values for the equal filter.
At run time when the user enters some value for Customer ID (say 'ABCD') we would replace the #InCustomerID by 'ABCD'
At runtime the query would be as follows:
http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=CustomerID eq 'ABCD' and Country eq 'US'
In the above case the user has entered the following values: CustomerID => 'ABCD' and Country => 'US'
My question is regarding handling of null values in OData $filter. If the user does not enter any value for CustomerID then we want to select all customers from specific country.
In sql case this would be something like:
select * from Customers where ((CustomerID = #InCustomerID) or (#CustomerID is null)) and (Country = #Country).
Essentially how to handle null or empty values so that the specific predicate in the logical condition would always be true.
Does OData filtering enables this option?
You can compare to null using the equality operator like this:
$filter=CustomerID eq null
In your case the query would degenerate to something like:
$filter=(CustomerID eq null) or (null eq null)
Which should work, but it's not very nice.
Did you consider removing the predicate completely in such case?