1

What is the most effective way to use null value in SQLite database treated as zero value in .NET calculations? I use Entity Framework.

Let's say I have:

Dim result as Double
result = e_employee_salary * e_index

where e_employee_salary and e_index and fields in the SQLite database but one or both of them can be null and in that case I want to treat them as zero value in the above calculation.

I'm looking the most effective way to do that since these calculations appear in many property getters all over and are called with high frequencies.

Nuts
  • 2,755
  • 6
  • 33
  • 78
  • Any reason why the null value couldn't be trapped from the query/sql perspective? Assuming isnull is a SQLite function: isnull(column_name, 0) will always return 0 (integer) when it is null. isnull(column_name, '0') will always return '0' (string) when column is null. Just a suggestion. – Tiny Haitian Aug 29 '13 at 20:29
  • http://stackoverflow.com/questions/799375/sqlite-equivalent-to-isnull-nvl-ifnull-or-coalesce – Tiny Haitian Aug 29 '13 at 20:33
  • I am using Entity Framework and not directly defining sql queries. I do LINQ to Entities instead to read data. – Nuts Aug 31 '13 at 19:44

2 Answers2

2

You may have two approaches: using programming language, as @Claudio or using SQLite query:

 SELECT IFNULL(e_employee_salary, 0.0), IFNULL(e_index, 0.0), ... FROM ...

This way you will always get non-null values from database.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
0

I could be missing something on your question, but I'd say you could use

If(e_employee_salary, 0) * If(e_index, 0) // VB

(e_employee_salary ?? 0) * (e_index ?? 0) // C#
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155