.NET Framework Bookmark and Share   
 index > Microsoft Codename 'Oslo' > Nullable value in query
 

Nullable value in query

Hello!

I have simple table with two dates: 'from' and 'to'. Date 'to' is optional.
I want to see all elements between dates 'from' and 'to' - if 'to' is specified and all elements greater than 'from' if 'to' is not specified.


Sample code:

module Shop
{
type PK : Integer64;

type SimpleDictionary
{
id : PK => AutoNumber();
name : Text#40;
displayname : Text#40;
active : Logical => true;
visible : Logical => true;
usefrom : DateTime;
useto : DateTime?;
} where identity (id), unique(name);

type KindType : SimpleDictionary
{
parent : KindType? where value in Kind;
};

Kind : KindType*;


AvailableKind(now : DateTime)
{
Kind.active(true)
where value.usefrom >= now
&& (value.useto ?? true : (value.useto < now)); // 1
//&& (value.useto == null || (value.useto != null && value.useto < now)); // 2
}
}

The problem is:
1 - error M0197: 'value.useto < now' cannot be used in a type context. To be used in a type context a value must be a collection.

2 - error M0102: Could not find binary operator '&&' with left-hand side type 'Logical' and right-hand side type 'Logical?'.

Haw to get required elements?

Thanks and Regards

ryszarddrozd

For 1, ?? is the coalesce operator. value.useto ?? true means: check value.useto. If it's not null, this expression evaluates directly to value.useto. If it is null, this expression evaluates to true. Since value.useto is DateTime? The expression returns either a DateTime or a Logical, which does not work. The : is being interpreted as ascription in this context.

I believe you want ? (the ternary operator) instead of ?? (coalesce operator). Here's an example for 1:
&& (value.useto == null ? true : ((value.useto : DateTime) < now)); // 1

For 2, explicitly casting value.useto to DateTime should work:
&& (value.useto == null || ((value.useto : DateTime) < now));

David

dmatson

For 1, ?? is the coalesce operator. value.useto ?? true means: check value.useto. If it's not null, this expression evaluates directly to value.useto. If it is null, this expression evaluates to true. Since value.useto is DateTime? The expression returns either a DateTime or a Logical, which does not work. The : is being interpreted as ascription in this context.

I believe you want ? (the ternary operator) instead of ?? (coalesce operator). Here's an example for 1:
&& (value.useto == null ? true : ((value.useto : DateTime) < now)); // 1

For 2, explicitly casting value.useto to DateTime should work:
&& (value.useto == null || ((value.useto : DateTime) < now));

David

dmatson
Thank You!

I didn't know the proper cast operator:

(value : Type)

Thank You again!
ryszarddrozd

You can use google to search for other answers

Custom Search

More Threads

• Intellipad Classification Attributes
• Starting an Open Source project based on MGrammar..
• Intellipad get ArgumentOutOfRangeException on Opening Mg Module
• "Polymorphic" schema elements?
• MGraph: References
• Shift reduce and reduce reduce conflicts
• New version of Intellipad ?
• CreateRepository just plain falls over...
• MGrammar: nest syntax and line termination
• Using an MGrammar generated parser for syntax highlighting