c# LINQ where with nullable boolean fields - linq

I have the following query:
where !(tf.Shipped.HasValue == true || tf.Ordered.HasValue == true || tf.Processed.HasValue == true)
Note that Shipped, Ordered and Processed are all nullable Boolean fields.
What I am trying to do is to check that if Shipped or Ordered or Processed have a value of true, they should NOT be in the result.
In my case Ordered is true but I am still getting this records. Not sure what I am doing wrong.

You're checking whether the nullable bools have a value.
If that value is false, HasValue will still be true.
You probably want to write
where !(tf.Shipped == true || tf.Ordered == true || tf.Processed == true)
Comparing nullable bools is the only case where one should write == true.
However, you probably should not be using nullable bools in the first place.
Unless you have a meaningful distinction between null and false, you should use regular bools instead and save yourself a lot of headache.

Related

CASE WHEN conditional in HiveQL

I'm having trouble bringing a case then into HiveQL.
I have a column with data:
if column is NULL = FALSE
if the column is PEOPLE = TRUE. It returns all results as TRUE.
What's wrong with my role?
SELECT
id,
datetime,
CASE
WHEN tb_people !="" THEN 'TRUE'
ELSE 'FALSE'
END
FROM <BD>.<TABLE>
!="" is only checking for blank strings. "" and NULL are two different things. As such, what you're looking for is:
SELECT
id,
datetime,
CASE
WHEN tb_people IS NOT NULL THEN 'TRUE'
ELSE 'FALSE'
END
FROM <BD>.<TABLE>
I'd also recommend using the boolean true and false rather than string literals. This would let you replace your entire case statement with ifnotnull(tb_people) https://hive.apache.org/javadocs/r3.0.0/api/org/apache/hadoop/hive/ql/exec/vector/expressions/IsNotNull.html.

If include? == false

I have an array and a string:
$header = ["Date", "Time", "Site Name", "Computer Name"]
columnName = "esfjk sdhf sdf"
and I am checking if $header contains columnName:
return if $header.include? columnName == false
The condition above always returns true, and the code carries on, even though the array doesn't contain the string.
I also have the same issue when $hash is a hash and recordNum is a number such as 99999999 which is not in it, and I do:
return if $hash.has_key? recordNum == false
Any reason for this happening?
Precedence.
$header.include? columnName == false
is interpreted as
$header.include?(columnName == false)
that is, usually,
$header.include?(false)
which is false. So what you want to do instead is this:
$header.include?(columnName) == false
But in your particular case, I would do this (thanks, Alex Wayne):
return unless $header.include?(columnName)
And if you do that, then you can go back to the parentheses-less form:
return unless $header.include? columnName

Linq check to see is there any NULLs in a set of DataRows?

I have a set DataRows and I want to check if any of the fields in any of those rows has a NULL value in it. I came up with this below, but I'm not sure because I'm nesting an ALL.
result.AsEnumerable().AsQueryable().All(o => o.ItemArray.All(i=>i == DBNull.Value))
Hard to tell because I can't put a "watch" in lambdas.
Actually you need to use Any (in your code you will return true if All values are null) and AsQueryable() is useless in this case.
bool nullFound = result.AsEnumerable()
.Any(o => o.ItemArray.Any(i=>i == DBNull.Value || i == null));
Then, If you need a list of all rows with some value null, just do the following:
var rowsWithNulls = result.AsEnumerable()
.Where(o => o.ItemArray.Any(i=>i == DBNull.Value || i == null))
.ToList();
P.S.
I also added a null check to be more safe, but if you are sure to have only DBNull.Value, you can remove it.
Not sure if this is the correct answer. I'm also rather new to Linq, but i believe you can do something like this;
result.AsEnumerable().AsQueryable().SingleOrDefault(o => o.ItemArray.All(i=>i == DBNull.Value))
This will return an list of items or null if there aren't any. Not sure if you can also nest it, but don't see why it wouldn't be possible

How Oracle 10g evaluates NULL in boolean expressions

if not (i_ReLaunch = 1 and (dt_enddate is not null))
How this epression will be evaluated in Oracle 10g
when the input value of the i_ReLaunch = null and the value of the dt_enddate is not null
it is entering the loop.
According to the rules in normal c# and all it should not enter the loop as
it will be as follows with the values.
If( not(false and (true))
= if not( false)
=if( true) which implies it should enters the loop
But it is not happening
Can someone let me know if i am wrong at any place
Boolean operations with NULL value in Oracle return UNKNOWN - not true or false. So you have something like this:
If( not(UNKNOWN and (true)) = if not( UNKNOWN) =if( UNKNOWN )
In this case, IF will treat UNKNOWN as false.
If i_relaunch can be null, then you need to use some of NULL handling functions(NVL, NVL2, NULLIF, COALESCE, LNNVL) to be sure that you have correct result.
See these article for more information:
Nulls: Nothing to Worry About
Fundamentals of PL/SQL. Scroll down to - Handling Null Values in Comparisons and Conditional Statements

Unpassable Where Clauses LINQ-to-SQL

As I'm struggling to learn LINQ I’ve managed to generate a SQL statement with "AND (0 = 1)" as part of the where clause. I'm just wondering if this result is common in poorly written queries and is a known issues to try and avoid or if I am doing something totally backwards to end up with this.
Update
public static IEnumerable<ticket> GetTickets(stDataContext db,string subgroup, bool? active)
{
var results = from p in db.tickets
where
( active == null || p.active == active )
/*(active == null ? true :
((bool)active ? p.active : !p.active))*/ &&
p.sub_unit == db.sub_units.Where(c=>subgroup.Contains(c.sub_unit_name))
select p;
return results;
}
If I ignore the active part and just run
public static IEnumerable<ticket> GetTickets1(stDataContext db,string subgroup, bool? active)
{
return db.tickets.Where(c => c.sub_unit.sub_unit_name == subgroup);
}
It returns the groups of tickets I want ignoring the active part.
I'd pull the processing out of the ternary operators.
where ( active == null || p.active == active )
EDIT
The rest of the where clause looks funky too... why is it not just doing
&& p.sub_unit.sub_unit_name == subgroup
or
&& subgroup.Contains(p.sub_unit.sub_unit_name)
?
That is some pretty heavy abuse of the ternary operator.
This expression:
(active == null ? true :
((bool)active ? p.active : !p.active))
Is equivalent to the following logic:
bool result;
if (active == null)
{
result = true;
}
else
{
if ((bool)active)
{
result = p.active;
}
else
{
result = !p.active;
}
}
result &= ...
Think carefully about what this is doing:
If active is null, you're fine, it skips to the next condition.
If active is true, result is true at the end of the conditional.
If active is false, result is false at the end of the conditional.
In the last case, the query can never return any rows!
#Tanzelax has already supplied a simple rewrite. The main idea is that you want to compare p.active to active, not actually evaluate the condition as p.active.
This is probably caused by a null value in one you the columns you have declared as non-nullable. LINQ2SQL makes all columns non-nullable by default. Go back to the designer and change the fields to allow values to be null. Unfortunately this feature is By Design (see connect.microsoft.com link below.)
(linq) incorrect sql generated for row.column == localVar when localVar is null (should be "is null" check)

Resources