Concatenate fields that isnullable

Posts   
 
    
Steven
User
Posts: 8
Joined: 07-Jan-2010
# Posted on: 22-Apr-2010 21:49:14   

i am using this line however HouseOwnerFields.Infix can can be null if it is null the whole string returns null and that shouldn't happen, in SQL i use ISNULL() to fix this but how do i do this in llblgen?


fields.DefineField(new EntityField("Fullname", (HouseOwnerFields.Firstname + ' ' + HouseOwnerFields.Infix + ' ' + HouseOwnerFields.Lastname)), 10);
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 22-Apr-2010 23:32:58   

Can you post the SQL that gets generated with this...?

Matt

Steven
User
Posts: 8
Joined: 07-Jan-2010
# Posted on: 23-Apr-2010 10:04:09   

I asume you mean the SQL equilant for this

SELECT 
      ([Firstname] + ' ' +
      ISNULL([Infix], '') + ' ' +
      [Lastname]) as Fullname
    
  FROM [D-Rent - RentalHouses - Trunk].[dbo].[HouseOwner]

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 23-Apr-2010 10:49:13   

If you use ISNULL() dbFunction in SQL, then you should also use a DBFunctinCall in code to emit the ISNULL() in SQL.

Steven
User
Posts: 8
Joined: 07-Jan-2010
# Posted on: 23-Apr-2010 11:05:21   

i am not that pro, can you give me a conrete example of how i have to do this in this situation?

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 23-Apr-2010 12:07:38   

SELECT ([Firstname] + ' ' + ISNULL([Infix], '') + ' ' + [Lastname]) as Fullname

FROM [D-Rent - RentalHouses - Trunk].[dbo].[HouseOwner]

Please try the following:

ResultsetFields fields = new ResultsetFields(1);
fields.DefineField(HouseOwnerFields.AnyField, 0, "fullName");

fields[0].ExpressionToApply = new DbFunctionCall(
    "([{0}] + ' ' +
     ISNULL([{1}], '') + ' ' +
     [{2}])", 
    new object[] { HouseOwnerFields.Firstname, HouseOwnerFields.Infix, HouseOwnerFields.Lastname});
Steven
User
Posts: 8
Joined: 07-Jan-2010
# Posted on: 26-Apr-2010 10:21:49   

thanks a lot! It worked with a little modification(without the []):


fields.DefineField(HouseOwnerFields.Firstname, 9, "FullName");

            fields[9].ExpressionToApply = new DbFunctionCall(
             "({0} + ' ' + ISNULL({1}, '') + ' ' + {2})",
              new object[] { HouseOwnerFields.Firstname, HouseOwnerFields.Infix, HouseOwnerFields.Lastname});

couldn't test it earlyer since i was not at work.