Closed rubenalves closed 1 year ago
at the very end of the file
I explained it wrong, i was loging on a example on how to create views, like,
var docs = ViewHelper.EntitySet
Do you have any examples like this? is it possible to make aggregate values?
Thanks, sorry for not explaing it bat.
There are several views defined in BooksModule.cs in BookStore project in Samples folder I think you can use Linq stuff, including aggregations/grouping
Problem solved, Thanks.
I have it working, is there any way to force the view to use default values?
Ex. QtdEnt01 = lanSet.Where(a => a.Artigo == art && a.MovimentaStock && a.TipoMovimento == TiposMovimento.Entradas && a.DataRegisto.Year == ac.Ano && a.DataRegisto.Month == 1).Sum(bol => bol.Quantidade),
Column QtdEnt01 is NULL, is there a way to make it be the default value? 0 for 0.00 for decimal?
Thanks.
as far as I understand, you get null value because there's no rows to Sum? then just check it for null and replace with 0 (if we are talking about c#). Or just add at the end (expr) ?? 0; If it is something in lower level, inspect the SQL, run it manually in SqlMgmtStudio, see what it returns, and can it be handled (there's COALESCE function I think) Otherwise I need more context here
Yes, it is null when there is nothing to sum, is it possible to do on the ViewDefinition?
Here is a Example:
var artigos = ViewHelper.EntitySet
RegisterView<IAcumArt>(query, DbViewOptions.None, "ArtigosAcum");
Tnaks.
wow, that's some View .... and you say it produces correct SQL? wow try adding " ?? 0,0 " after the Sum; it should be translated into Coalesce(sum, 0.0) in SQL
Yes, it produces the correct SQL, I canot add ?? 0 because it does not compile.
put all preceding expression starting from IanSet into parenthesis:
QtdComp12 = (
lanSet.Where(a => a.Artigo == art && a.MovimentaStock && a.TipoMovimento == TiposMovimento.Compras &&
a.DataRegisto.Year == ac.Ano && a.DataRegisto.Month == 12).Sum(bol => bol.Quantidade)
) ?? 0.0m,
if 'does not compile', tell me the error
It does not compile, gives a error.
Operator ?? canot be applied to of type decimal and decimal
Left operatior of the ?? operator must be of reference or nullable type
That is the i get in visual studio.
ah, get it. just for experiment, put "?" before the .Sum:
QtdComp12 = (
lanSet.Where(a => a.Artigo == art && a.MovimentaStock && a.TipoMovimento == TiposMovimento.Compras &&
a.DataRegisto.Year == ac.Ano && a.DataRegisto.Month == 12)?.Sum(bol => bol.Quantidade)
) ?? 0.0m,
Does not compile.
again, what's the error?
Severity Code Description Project File Line Suppression State Error CS8072 An expression tree lambda may not contain a null propagating operator. TsGest.DL C:\Dropbox\TerceiraSoft\2021\TsGest\TsGest.DL\TsGestModules\TsGestModule.cs 151 Active
That is th error.
Ok, let's try to use custom code snippet to inject IsNull call into SQL. First, have a look at example of defining snippet, in test file MiscTests_Model.cs, in basic tests, at the very end, function DbAddYears:
We will try to do something like this for IsNull function in MsSqL. Define the following ext method in a static file:
// MS SQL IsNull function: https://www.w3schools.com/sql/func_sqlserver_isnull.asp
[SqlExpression(DbServerType.MsSql, "ISNULL({value}, 0)")]
public static Decimal NullAsZero(this Decimal value) {
CannotCallDirectly();
return default;
}
private static void CannotCallDirectly([CallerMemberName] string methodName = null) {
throw new NotImplementedException(
$"Function '{methodName}' may not be called directly, only in LINQ expressions.");
}
continued, pushed Post by accident, sorry
now, call this NullAsZero on the result of sum:
QtdComp12 = lanSet.Where(a => a.Artigo == art && a.MovimentaStock && a.TipoMovimento == TiposMovimento.Compras &&
a.DataRegisto.Year == ac.Ano && a.DataRegisto.Month == 12).Sum(bol => bol.Quantidade)
.NullAzZero();
let's try this; the resulting SQL should have this IsNull() call around Sum
it does not work, gives this error: Function NullAsZero not supported in queries
I will try to alterar the view after vita created it, will informe you of the result.
I missed one thing, you need to register the class containing this function, in the module constructor where you register all entity types:
mainModule.RegisterFunctions(typeof(YOUR_CLASS_WITH_EXT_METHOD));
See BooksModule for example
Correction: you can do it module constructor or in EntityApp constructor, as in Books sample app
It works, You are The MAN.
Thanks for making the best ORM ever.
Yaaay!!! congrats!
Hello,
I have used ViewHelper.EntitySet in a old project and i canot find eny examples oh how to use it, can you point me for any examples i can analise if?
Thanks.