Hi,
I have this code that return a value if a record exist or not , but one of the parameters is in error:
"destinationrecord is not null here" , this parameters is "destinationRecord.IdMovpartite"
C#
var dbValue = _dbContext.GetEstrazioneVenditeByIdAziIdDocIdRig(destinationRecord.IdAzienda, destinationRecord.IdDocumento, destinationRecord.IdRiga, destinationRecord.IdMovpartite);Is there a way to make this check with a parameters that could be null or not?
The goal is,how can i retrieve dbValue with the 4 keys where 1 of this coul be null?
Thanks in advance!

Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:43 AM EDT
The approach assumes that the GetEstrazioneVenditeByIdAziIdDocIdRig method supports passing null for the nullable parameter. If the method doesn't handle null values properly or if the database schema doesn't allow nulls in that parameter, the approach may not work as expected.
Aymen AmriRank #1,022 · 1.9K reputationPosted Jun 17, 2023, 5:37 AM EDT
It seems that somewhere in your code you are assigning null to a non-nullable value.
For example if you have a variable that is declared as long "long a;" it won't be able to hold null.
you need to declare your property with "?" for example "long? a;"
Deepak RawatRank #646 · 3.7K reputationPosted Jun 18, 2023, 5:37 AM EDT
To handle a situation where one of the parameters can be null when retrieving
dbValue, you can modify your code to conditionally include the nullable parameter in the query. Here's an example:var dbValue = _dbContext.GetEstrazioneVenditeByIdAziIdDocIdRig( destinationRecord.IdAzienda, destinationRecord.IdDocumento, destinationRecord.IdRiga, destinationRecord.IdMovpartite ); // If destinationRecord.IdMovpartite is null, try the query without including the parameter if (dbValue == null && destinationRecord.IdMovpartite == null) { dbValue = _dbContext.GetEstrazioneVenditeByIdAziIdDocIdRig( destinationRecord.IdAzienda, destinationRecord.IdDocumento, destinationRecord.IdRiga, null // Pass null explicitly for the nullable parameter ); } // Handle the result (dbValue) accordinglyIn the code above, we first try to retrieve
dbValueusing all four parameters. If it returns null and thedestinationRecord.IdMovpartiteis also null, we retry the query by explicitly passing null for the nullable parameter.This way, you can retrieve
dbValuewith the four keys, even if one of the parameters (destinationRecord.IdMovpartite) can be null. Adjust the logic as per your specific requirements and handle the result accordingly in your code.Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:42 AM EDT
The code becomes more complex with additional conditional checks and duplicated queries. This can make the code harder to understand, debug, and maintain. Also depending on the database schema and the meaning of the parameters, passing null for a parameter might result in different data being retrieved.
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:43 AM EDT
If the database query is expensive or time-consuming, executing it twice (once with all parameters and once with a subset of parameters) can have a performance impact. This might not be noticeable for small datasets or simple queries, but it could become an issue for larger datasets or complex queries.