2
votes

I'm trying to use the feature documented here : https://github.com/ServiceStack/ServiceStack.OrmLite#custom-sql-customizations

This is how I'm using it:

var q = Db.From<MemberAccess>().LeftJoin<Member>();
return Db.Select<MemberResponse>(q);

Response object:

public class MemberResponse
{
    public Guid Id { get; set; }

    public string MemberFirstName { get; set; }

    public string MemberLastName { get; set; }

    public string MemberEmail { get; set; }

    [Default(OrmLiteVariables.SystemUtc)]
    public string AccessedOn { get; set; }

    [CustomSelect("CONCAT(LEFT(Member.FirstName, 1),LEFT(Member.LastName,1))")]
    public string MemberInitial { get; set; }
}

It seems like whatever I put in CustomSelect doesn't get used.
Maybe, I'm not using this correctly?
Also, the Default attribute doesn't work either.
I tried that as it was an example from the doco.

Any idea will be appreciated.

Thanks in advance.

1

1 Answers

2
votes

The [CustomSelect] only applies to the source table. Selecting the results in a custom type is used to map the returned resultset on the MemberResponse type, it doesn't have any effect on the query that gets executed.

Likewise with [Default(OrmLiteVariables.SystemUtc)] that's used to define the default value when creating the table which is only used when it creates the Column definition, so it's only useful on the source Table Type.

Both these attributes should only be added on the source MemberAccess to have any effect, which your mapped MemberResponse can access without any attributes, e.g:

public class MemberResponse
{
    public Guid Id { get; set; }

    public string MemberFirstName { get; set; }

    public string MemberLastName { get; set; }

    public string MemberEmail { get; set; }

    public string AccessedOn { get; set; }

    public string MemberInitial { get; set; }
}

Sql.Custom() API

The new Sql.Custom() API added in v4.5.5 that's available on MyGet will let you select a custom SQL Fragment, e.g:

var q = Db.From<MemberAccess>().LeftJoin<Member>()
  .Select<MemberAccess,Member>((a,m) => new {
    Id = a.Id,
    MemberFirstName = m.FirstName,
    MemberLastName = m.LastName,
    MemberEmail = m.Email,
    MemberInitial = Sql.Custom("CONCAT(LEFT(Member.FirstName,1),LEFT(Member.LastName,1))")
  });
return Db.Select<MemberResponse>(q);