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

So, I need to make a request where I need the NVL group, but I need to do it in linq (if this helps the db backend is BD2, and we use subsonic), I searched the Internet for β€œNVL linq” and didn’t actually find nothing useful so i ask here

Thank you for your help...

+4
source share
2 answers

Can you use the zero 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 zero, it uses the value on the right. So, in the example, if row.a is null, then a becomes "default" .

This assumes row.a is a string.

+8
source

If someone wants to know how I did this, I did not use the null coalesce operator ... It was even easier. Maybe I didn’t explain myself, but I had to say that if the value is null, then I want to include this result. Check this.

  possibleVendors = (from vndMapping in db.COMPANIES_VND_MAPPINGS join v in db.COMPANIES_CMP_COMPANIES on vndMapping.VENDOR_ID equals v.COMPANY_ID where !(from ex in db.COMPANIES_VND_MAPPINGS where (ex.OEM_ID == oemId || ex.OEM_ID == null) && (ex.MODEL_ID == modelId || ex.MODEL_ID == null) && (ex.MODALITY_ID == modalityId || ex.MODALITY_ID == null) && (ex.CLASS_ID == productTypeId || ex.CLASS_ID == null) && ex.EXCLUDE.ToUpper().Equals("Y") select ex.VENDOR_ID).Contains(vndMapping.VENDOR_ID) && (vndMapping.OEM_ID == oemId || vndMapping.OEM_ID == null) && (vndMapping.MODEL_ID == modelId || vndMapping.MODEL_ID == null) && (vndMapping.MODALITY_ID == modalityId || vndMapping.MODALITY_ID == null) && (vndMapping.CLASS_ID == productTypeId || vndMapping.CLASS_ID == null) select new { vndMapping.VENDOR_ID, v.COMPANY_NAME }).Distinct().OrderBy(x => x.VENDOR_ID).ToDictionary(x => x.VENDOR_ID, x => x.COMPANY_NAME); 

Now keep in mind - I had a few limitations in that I did not design the business logic (obviously) or the database. Someone might have a better way to do this, if you had full control of everything, this seems to work.

0
source

Source: https://habr.com/ru/post/1346925/


All Articles