Accepted answer


var recent = from p in
        orderby p.modtime descending
        where p.status == "current"
        select new
            rsub = (p.subnumber).tostring(),
            rnumber = (p.streetnumber).tostring(),
            rnum = string.isnullorempty((p.subnumber).tostring()) ? (p.streetnumber).tostring() : (p.subnumber).tostring() + "/" + (p.streetnumber).tostring(),
            rstreet = p.street,
            rsuburb = p.suburb,
            rurl = p.propertyurl,


just a ternary conditional aka ?: operator in the "select" should do:

select new
    house = p.subnumber != null
        ? p.subnumber + "/" + p.streetnumber
        : p.streetnumber;

this makes the assumption street number is always there (or it might result in "xxx/" or null). it also assumes that sub is null (not just empty) if truly not present.

if it starts to get "too complicated", consider the following (which has slightly different rules than above, those are left to be figured out):

select new
    house = prettyhousenumber(p.subnumber, p.streetnumber),

string prettyhousenumber(string sub, string street) {
    // ?: could also be used here as well, but since invoking the method
    // can be used as an expression itself, breaking it up like this also
    // allows the use of other constructs
    if (!string.isnullorempty(sub)) {
        return sub + "/" + street;
    } else {
        return "" + street; // null will go to "", if it can even ever come up

which should show how any expression, including a method call, can be used there -- pass it some data and get some data back :) while there are limits with expression trees and which ones can be efficiently turned into sql, since this is just processing data already returned then there is nothing to worry about here.

happy coding.


on the table level you could create a computed column; these are not stored - the value is 'created' when the column is queried.

create table [customer]
     [subnumber] nvarchar(256),
     [streetnumber] nvarchar(256),
     [fulladdress] as (case 
                      when [subnumber] is null then [streetnumber]
                      else [subnumber] + n' ' + [streetnumber]

or you can add it the table:

alter table [customer]
   add column [fulladdress]
       as (case 
            when [subnumber] is null then streetnumber
            else [subnumber] + n' ' + streetnumber

now the value will be directly-accessible from your ef model.

Related Query

More Query from same tag