-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document difference between First and FirstOrDefault() #323
Comments
First
and FirstOrDefault()
Simpler reproducible case: using Starcounter;
using System.Linq;
namespace StarcounterApplication1
{
[Database]
public class Person
{
public decimal Money { get; set; }
}
class Program
{
static void Main()
{
Handle.GET("/Hello", () =>
{
var result = Db.SQL<decimal>($"SELECT SUM(p.Money) FROM {typeof(Person)} p");
var first1 = result.First;
var first2 = result.FirstOrDefault();
return "done";
});
}
}
} The line
|
Ouch. It looks like we are back with the issue discussed at length in https://github.com/Starcounter/level1/issues/2134 In short: aggregations for MIN, MAX, SUM might internally return null, while a default value for a generic valuetype cannot be null. Not sure what is a feasible solution for this problem. @miyconst: discuss it tomorrow in the release planning perhaps? |
Nullable types do not work with calculated properties: public decimal? CurrentBalance =>
Db.SQL<decimal?>("SELECT SUM(e.Amount) FROM Expense e WHERE e.Spender = ?", this).FirstOrDefault(); Throws:
|
@Mackiovello I believe that by "calculated properties" you meant the bound properties in Typed JSON, which is not the problem of The nullable values are not supported by Typed JSON, so the property declaration should be changed to: public decimal CurrentBalance =>
Db.SQL<decimal?>("SELECT SUM(e.Amount) FROM Expense e WHERE e.Spender = ?", this).FirstOrDefault() ?? 0; |
The MS-SQL returns NULL in this case, the same Starcounter does, and it fails when trying to convert NULL into required data type ( For me everything is good enough to keep as is. cc @k-rus |
The blog post has been updated to reflect this. Closing |
Yes, that is correct, and I cc you to make sure there is nothing extra you would like to add. |
The class
QueryResultRows
has a propertyFirst
. This property was deprecated in 2.3.1 in favor of Linq'sFirstOrDefault
method.After checking out the Starcounter HelloWorld app and changing
First
toFirstOrDefault()
on line 14 I get an exception when going tolocalhost:8080/HelloWorld
:Changing back to
First
makes it work.What's the difference between
First
andFirstOrDefault()
? Does it handlenull
differently?I'm on Starcounter 2.3.1.7870
The text was updated successfully, but these errors were encountered: