Rashim's Blog

Beauty of Partial Class!!!

Posted on: January 9, 2012

Say i have a Database table name Employee and has 4 columns like ID, Firstname, Lastname,Email etc. That is, there is no Fullname Column but we need it to display it on the UI. So what we usually do?
We fetch all data; iterate over the list of data to make the Fullname String, right?
But there is another nice way to do that, if we get the property Fullname like Firstname or Lastname then it might help us a lot for further processing.
What I did for that, I have an Employee class in my dbml file. So I have created a partial Class name Employee in which I have define a Property Fullname as read-only. So when we get the Object Employee we will get the property Fullname also.

public partial class Employee
{
    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", this.Firstname, this.LastName);
        }
    }
}

Very Simple but Handy !!!!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: