开发者

How to concat strings in LINQ while properly dealing with NULL values

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();


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.


You can use the ?? operator this way :

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


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


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜