I'd like an elegant way to concatenate several columns together using LINQ, but using the + operator or concat() when any of the columns are NULL results in NULL for the value after concatenation.
Is there anything similar to concat() that handles NULL differently, or am I thinking about this in the incorrect way?
Any help is appreciated!
Here is the code I am using:
List<CustomObject> objects = (
from obj in ObjectTable
where obj.Id == Id
select new CustomObject()
{
EnteredBy = obj.EnteredBy,
EntryDate = obj.EntryDate,
WorknoteText =
obj.VchWorkNote1 +
obj.VchWorkNote2 +
obj.VchWorkNote3 +
obj.VchWorkNote4 +
obj.VchWorkNote5 +
obj.VchWorkNote6 +
obj.VchWorkNote7 +
obj.VchWorkNote8 +
obj.VchWorkNote9 +
obj.VchWorkNote10 +
obj.VchWorkNote11 +
obj.VchWorkNote12 +
obj.VchWorkNote13 +
obj.VchWorkNote14 +
obj.VchWorkNote15 +
obj.VchWorkNote16 +
obj.VchWorkNote17 +
obj.VchWorkNote18 +
obj.VchWorkNote19 +
obj.VchWorkNote20
}).ToList();
Can you add a new column to your database? Something like "Keywords" or "FullText"
Define it to have a calculation, that calculation is basically
"ISNULL(<Field1>, '') + ISNULL(<Field2>, '')"
etc.Make sure to mark it as persisted, so it doesn't have to calculate each time.
Then you just need to pull down that one field from your table.
How about
(object.VchWorkNote1 ?? "") +
.....You can use the ?? operator this way :
there's probably a cleaner way, but first thing that comes to mind would be a quick null to zero length string conversion:
One option is to use the null coalescing operator:
Hopefully the generated SQL will use an appropriate translation.