Comparator.comparing(((Function<Entity, SubEntity>)Entity::getSubEntity).andThen(SubEntity::getAmount))); - java-8

what if SubEntity is null, some of of SubEntity itself null then SubEntity::getAmount null pointer so how to avoid

use Comparator.nullsLast
Comparator.nullsLast(
Comparator.comparing(((Function<Entity, SubEntity>)Entity::getSubEntity)
.andThen(SubEntity::getAmount))));

Related

How to pass a null value as bind parameter

I have this table Line with a DEL_IND column. The possible values are Y or null
Im using OBIP, and there is a parameter requirement that allows selection of null or Y.
OBIP do not allow blank in their 'fixed value' menu.
I've tried to enter 'List of Values' in OBIP for No to be '' (empty string), but it doesnt seem to work.
LINE.DEL_IND = :P_DELETION_FLAG << i need to pass the value null for this clause
How do I pass null value selection into the query?
Even if you find the way to pass NULL, this:
WHERE LINE.DEL_IND = :P_DELETION_FLAG
won't work properly. If :P_DELETION_FLAG is NULL, query should look like this:
WHERE (LINE.DEL_IND = :P_DELETION_FLAG or :P_DELETION_FLAG IS NULL)
because
WHERE LINE.DEL_IND = NULL
is invalid; should be
WHERE LINE.DEL_IND IS NULL (or IS NOT NULL)

Getting MinDate instead of NULL

I am calling a stored procedure, and may expect a NULL back.
However, when I get to EntityFramework with the result, it seems that the result gets converted to a Min Date (01-01-0001).
result.NextPaymentDate =
(from c in Context.GetPaymentDatesForSchedule(source.id) where c.NextPaymentFlag select c.PaymentDate)
.FirstOrDefault();
Is there a way to make it NULL, if I get a NULL value from the sproc?
It seems 'dirty' doing something like:
if (result.NextPaymentDate == DateTime.MinValue)
result.NextPaymentDate = null;
It is because the NextPaymentDate property is not nullable in your model, if you make it nullable you will then get the null value instead of min date.

Unique from list or null in Linq?

I have a list of strings, and want to use a single line of Linq to return the list's unique value (if there is one) or null, otherwise. In other words:
["a","a","a","a","a"] returns "a"
["a"] returns "a"
["a","a","a","a","b"] returns null
["a","b","c"] returns null
[] returns null
I assume I would use IEnumerable.Distinct() to collapse the list to its unique values, but I don't know how to do the "single or null" (SingleOrDefault() throws an exception if there's more than one item; FirstOrDefault() will always return the first item, and won't return null if there's two or more like I want.)
Any ideas? I'm just really curious if there's a simple way to do this in a single line that I'm overlooking. Bonus points if you don't have to write an extension method to make it work. Thanks!
col.Distinct().Count() == 1? col.First() : null;
Would this do?
col.Distinct().Count() > 1 ? null : col.Distinct().First();
Updated to handle empty collection
col.Count() == 0 ? null : col.Distinct().Count() > 1 ? null : col.Distinct().First();

LINQ query - null date

Ive a simple linq query to return records with a null date field,
just want to check the synatx of the "where" line is ok
var query2 = from cs in db.tblCases
where cs.date_closed == null
etc, etc,
thanks again
DD
I would be careful with using null, I have seen issues with linq not generating the correct sytnax (ex IS NULL vs ==null)
I would recommend
var query2 = from cs in db.tblCases where !cs.date_closed.HasValue etc, etc,
Assuming your date_closed property is of a nullable type, e.g. Nullable<DateTime> aka DateTime?, that should be fine.

Make Linq return object or null

Is there a way to get the code below to return null if no objects are found?
var news = (from c in childs
where c.Name.ToLower().Contains("folder")
select c).First();
You want to use FirstOrDefault() instead of First(). It will do exactly what you want.
You should call FirstOrDefault<T>, which will return default(T) if there are no elements.
default(T) will be null for reference and nullable types, 0 for numeric types (byte, int, double, etc), and new T() for structures (which cannot be null)

Resources