✅ Linq 'nullable objects must have value' error source
I'm attempting to build up a query but I'm having issues with this group by.
When I try to perform
.ToListAsync()
I get the desired results but for further code reasons .CountAsync()
needs to also be functional but I keep getting this error.
-The TenantId is a nullable field but in the database there is 0 records where it's actually null.
-In this screenshot I'm setting the whole group g.ToList()
but in reality I only need access to the Name field and TenantId, pre-encountering this it was g.Select(x => x.Name).ToList()
I have tried quite a few things but can't seem get rid of the error, even knowing the source of the problem would be helpful.13 Replies
why don't you use
string.IsEmpty(f.Name)
instead of != string.Empty
can you verify just the data, like cut the query at the where
and look that everything is alrightThe DB provider probably can't translate
string.IsEmpty
would this use linq expressions? mmm
Well, they have a field called
_dbProvider
And they're also using query syntax for some reason
And anonymous objectsit's fiiiine
And if you look at the inline type hints, it says
IQueryable
If i rewrite it as method syntax it results in the same problem.
I can leave this filter out, each record in fact contains a string.
The provider can translate it because .ToListAsync works perfectly fine with that piece in it.
i don't know, maybe try posting the exception
here's the stacktrace with a few elements hidden for privacy reasons, the error message is in the title "Nullable object must have a value."
dunno could you try
.Select(g => new { TenantId = g.Key.Value, FeatureList = g.Any() ? g.ToList() : null });
null or Array.Empty, whateverI have tried this and the result remains the same where I can only generate a list and no count
have you then tried reproducing this in a minimal project
Began on it earlier but had to leave pc, will continue on it tomorrow
Can probably mark this as solved. In my console app in memory and exact replica of data my query works fine for both ToListAsync() and .CountAsync(). There will be another thing causing my problem.