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

Kenn picture Kenn · Apr 6, 2011 · Viewed 8.1k times · Source

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...

Answer

Matt Ellen picture Matt Ellen · Apr 6, 2011

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.