Caveman's Blog

My commitment to learning.

Linq: How to return a custom type

leave a comment »

In this blog post I am going to show you the C# and VB.Net ways of creating/returning custom typed objects. Let us create an Employee class that will hold our data. Our employee class has three properties Name, Age and Salary. Observe the new feature called “Automatic properties” that only requires the partial code to be written while defining properties. Let us define two constructors, one with arguments and another without arguments (default). The next step is to create a list of these employees. I have defined 6 employee objects via calling both the constructors, followed by adding each of these objects to the Employees typed List object.

    public class employee
        //Automatic properties
        public int Age { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }

        public employee();
        public employee(int age, string name, int salary)
        { this.Age = age; this.Name = name; this.Salary = salary; }

We now have a queriable object that can be used to returned a custom object(s). The query in the example below returns all employees who are older than 22 years and the same time, the employee salary is also recomputed on the fly. The Linq query is made to return the employee Name and Salary as a new object that is different that the Employee class/object that we have defined above.

    class Program
        static void Main(string[] args)
            List<employee> Employees = new List<employee>();
            //old school object instatiation
            employee e = new employee(20, "John", 100);
            //object Initializers
            e = new employee { Salary = 200, Name = "Smith", Age = 21 };
            e = new employee(22, "Mark", 300);
            e = new employee(23, "Dave", 400);
            e = new employee(24, "Jeff", 500);
            e = new employee(25, "Doug", 600);

            var Query = from tmp in Employees
                        where tmp.Age > 22
                        select new
                            EmployeeFirstName = tmp.Name,
                            EmployeeSalaryPlusCommision = (tmp.Salary + (2 * tmp.Salary))

            foreach (var q in Query)
                Console.WriteLine("Name: {0}; Salary: {1}", q.EmployeeFirstName, q.EmployeeSalaryPlusCommision);

Following is the VB.Net implementation of the above code.

Public Class employee
    Public Property Age() As Integer
            Return m_Age
        End Get
        Set(ByVal value As Integer)
            m_Age = Value
        End Set
    End Property
    Private m_Age As Integer
    Public Property Name() As String
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property Salary() As Integer
            Return m_Salary
        End Get
        Set(ByVal value As Integer)
            m_Salary = Value
        End Set
    End Property
    Private m_Salary As Integer
    Public Sub New()
    End Sub
    Public Sub New(ByVal age As Integer, ByVal name As String, ByVal salary As Integer)
        Me.Age = age
        Me.Name = name
        Me.Salary = salary
    End Sub
End Class

Module Module1

    Sub Main()
        Dim Employees As New List(Of employee)()
        'old school object instatiation
        Dim e As New employee(20, "John", 100)
        'object Initializers
        e = New employee() With { _
        .Salary = 200, _
        .Name = "Smith", _
        .Age = 21 _
        e = New employee(22, "Mark", 300)
        e = New employee(23, "Dave", 400)
        e = New employee(24, "Jeff", 500)
        e = New employee(25, "Doug", 600)

        Dim Query = From tmp In Employees Where tmp.Age > 22
        Select New With { _
        .EmployeeFirstName = tmp.Name, _
        .EmployeeSalaryPlusCommision = (tmp.Salary + (2 * tmp.Salary)) _

        For Each q As Object In Query
            Console.WriteLine("Name: {0}; Salary: {1}", q.EmployeeFirstName, q.EmployeeSalaryPlusCommision)

    End Sub

End Module

PS: Check this Developer Fusion, a website that helped me convert my C# code to Vb.Net



Written by coolgirlsblog

March 28, 2011 at 9:35 am

Posted in Dotnet, Linq

Tagged with , ,

Leave a Reply

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

You are commenting using your 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: