4

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();
2
  • Wow, this is insane. You should totally add a calculating column, like @taylorn suggested. Commented Apr 7, 2011 at 20:07
  • (string)null + null -> "", what am I missing here? Commented Apr 7, 2011 at 20:28

5 Answers 5

14

One option is to use the null coalescing operator:

List<CustomObject> objects = (from o in ObjectTable
                              where o.Id == Id
                              select new CustomObject(){
                              EnteredBy = o.EnteredBy, 
                              EntryDate = o.EntryDate, 
                              WorknoteText = 
                              (o.VchWorkNote1 ?? "") +
                              (o.VchWorkNote2 ?? "") + 
                              (o.VchWorkNote3 ?? "") +
                              (o.VchWorkNote4 ?? "") +
                              ...
                              (o.VchWorkNote20 ?? "")
                              }).ToList();

Hopefully the generated SQL will use an appropriate translation.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use the ?? operator this way :

...
(object.VchWorkNote5 ?? "") +
(object.VchWorkNote6 ?? "") +
(object.VchWorkNote7 ?? "") +
...

Comments

2

How about (object.VchWorkNote1 ?? "") +.....

Comments

2

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.

Comments

0

there's probably a cleaner way, but first thing that comes to mind would be a quick null to zero length string conversion:

List<CustomObject> objects = (from object in ObjectTable
                         where object.Id == Id
                     select new CustomObject(){
                         EnteredBy = object.EnteredBy, 
                         EntryDate = object.EntryDate, 
                         WorknoteText = 
                         object.VchWorkNote1 ?? "" +
                         object.VchWorkNote2 ?? "" + 
                         object.VchWorkNote3 ?? "" +
                         object.VchWorkNote4 ?? "" +
                         object.VchWorkNote5 ?? "" +
                         object.VchWorkNote6 ?? "" +
                         object.VchWorkNote7 ?? "" +
                         object.VchWorkNote8 ?? "" +
                         object.VchWorkNote9 ?? "" +
                         object.VchWorkNote10 ?? "" +
                         object.VchWorkNote11 ?? "" +
                         object.VchWorkNote12 ?? "" +
                         object.VchWorkNote13 ?? "" +
                         object.VchWorkNote14 ?? "" +
                         object.VchWorkNote15 ?? "" +
                         object.VchWorkNote16 ?? "" +
                         object.VchWorkNote17 ?? "" +
                         object.VchWorkNote18 ?? "" +
                         object.VchWorkNote19 ?? "" +
                         object.VchWorkNote20 ?? ""
                     }).ToList();

1 Comment

This is incorrect. The plus operator has higher priority than the ?? operator. You should add parenthesis around (object.VchWorkNote1 ?? "")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.