Two columns as one?

Posts   
 
    
bertcord avatar
bertcord
User
Posts: 206
Joined: 01-Dec-2003
# Posted on: 23-Dec-2004 15:05:22   

In one of my projects I have SQL code like this

SELECT FirstName + ' ' LastName AS FullName, FirstName, LastName FROM Users

I would like to do this in code is it possible?

Bert

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 23-Dec-2004 15:20:27   

Not yet, will be added soon. The options you have now is to add a property to the entity class.

Frans Bouma | Lead developer LLBLGen Pro
bertcord avatar
bertcord
User
Posts: 206
Joined: 01-Dec-2003
# Posted on: 08-Jan-2005 23:51:00   

Otis wrote:

Not yet, will be added soon. The options you have now is to add a property to the entity class.

Any way to do this with dynamic lists? Maybe add a ExOp.ConCat? I started looking at your source and it made my head hurt disappointed

So I think I need an ExOp.ConCat...

you could use it like so


IExpression lastname = new Expression(
                EntityFieldFactory.Create(UsersFieldIndex.LastName),
                ExOp.ConCat, "', '");

            IExpression fullname = new Expression(
                lastname,
                ExOp.ConCat,
                EntityFieldFactory.Create(UsersFieldIndex.LastName));

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 09-Jan-2005 12:47:07   

Use ExOp.Add simple_smile In SqlServer, if you do 'foo' + ' ' + 'bar' you'll get "foo bar" simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cour avatar
Cour
User
Posts: 12
Joined: 21-Jan-2005
# Posted on: 28-Jan-2005 01:57:33   

Otis wrote:

Use ExOp.Add simple_smile In SqlServer, if you do 'foo' + ' ' + 'bar' you'll get "foo bar" simple_smile

I'm using MySql so the above will not work. For mysql we have to use concat_ws(firstname, lastname)

Is there a way to pull this off? If not, you mentioned adding a property. By property do you mean:


public class OwnerEntity : OwnerEntityBase
....
public string FullName
{
      get { return this.FirstName+" "+this.LatName; }
} 

How do I use this property with dynamic lists? A bit of code would help, if you can. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 28-Jan-2005 10:12:30   

In a dynamic list it's easy: as you're working with a datatable, you can add a DataColumn which Expression property you set to something like "Firstname + ' ' + LastName". Do this after you've fetched the data into the datatable simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cour avatar
Cour
User
Posts: 12
Joined: 21-Jan-2005
# Posted on: 28-Jan-2005 19:04:19   

disappointed Talk about clueless. Duh.

First I suddenly realized that I can't use dynamic lists since that'll query the DB and since I'm editing in memory my collections, well every query will show what's currently in the DB and not in memory. So I have to stick to my collection.

What I hadn't realized at the time was that by creating a property (Fullname) on the entity and databinding the collection directly to the datagrid, that it automatically generates (besides all the entity fields) the column with the property Fullname I had created!! I never saw it until I horizontally scrolled the datagrid by mistake and voila! There it was! I felt quite stupid to say the least. simple_smile

So all I had to do now was set the datagrid's autogenerate=false and individually bind the columns I wanted to see with the datagrid's property builder. It was a piece of cake.

Anyways, I thought I'd explain this in case someone out there besides me didn't realize this.