Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know how to reproduce an NVL() function in linq

Tags:

linq

nvl

So I need to do a query where I need a bunch of NVL's but I need to do these in linq (if it helps the db backend is BD2 and we are using subsonic) I searched the web for "NVL linq" and didn't really find anything useful so I am asking here,

Thanks for your help...

like image 858
Kenn Avatar asked Apr 06 '11 14:04

Kenn


People also ask

Which is better COALESCE or NVL?

The advantage of the COALESCE() function over the NVL() function is that the COALESCE function can take multiple alternate values. In simple words COALESCE() function returns the first non-null expression in the list.

Does LINQ ever return null?

It will return an empty enumerable. It won't be null.

What is the purpose of the NVL function?

NVL. The NVL function allows you to replace null values with a default value. If the value in the first parameter is null, the function returns the value in the second parameter. If the first parameter is any value other than null, it is returned unchanged.

What is NVL and nvl2 function and difference?

What is the difference between nvl and nvl2? Answer: The nvl function only has two parameters while the nvl parameter has three arguments. The nvl2 like like combining an nvl with a decode because you can transform a value: NVL ( expr1 , expr2 ): If expr1 is null, then NVL returns expr2.


1 Answers

You can use the null coalescing operator ??:

var abs = from row in table
          select new {a = row.a ?? "default", b = row.b};

The operator looks at the value on the left and if it is null then it uses the value on the right. So in the example if row.a is null, then a becomes "default".

This assumes that row.a is a string.

like image 93
Matt Ellen Avatar answered Oct 29 '22 18:10

Matt Ellen