These are the modifications I would make to the Product aggregate if I wanted it to ignore NULL
s.
Change the attribute:
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(
Microsoft.SqlServer.Server.Format.Native,
IsInvariantToDuplicates = false,
IsInvariantToNulls = true, // receiving a NULL value will be ignored
IsInvariantToOrder = true,
IsNullIfEmpty = true,
Name = "Product"
)]
Change Accumulate
:
public void Accumulate(System.Data.SqlTypes.SqlDouble number) {
if (!this.HasValue && !number.IsNull) { //Don't know if we'll be passed a NULL, but protect ourselves nonetheless
this.Result = number;
} else if (number.IsNull) {
return; //Avoid setting HasValue
} else {
this.Result = System.Data.SqlTypes.SqlDouble.Multiply(this.Result, number);
}
this.HasValue = true;
}
Change Merge
:
public void Merge(Product group) {
if (group.HasValue) {
if(this.HasValue) {
this.Result = System.Data.SqlTypes.SqlDouble.Multiply
(this.Result, group.Result);
} else { //We may never have had our own value set
this.Result = group.Result;
this.HasValue = true;
}
}
}
I'm not sure if the change to Merge
is truly needed, but I'd do it for safety's sake.