Adding 2 sql table columns value to one C# property, using Dapper.NET

.net c# dapper

Question

I have a class Department.

Deparment has one property called Address.

I have a SQL table called Deparment.

This Department has 2 columns Address1 and Address2.

I use Dapper.net and would like to put the two SQL table columns into the one property of my class.

How ? Thank!

Accepted Answer

I would do:

public class Department {
    public string Address1 {get;set;}
    public string Address2 {get;set;}

    public string Address {
        get { return Address1 + Environment.NewLine + Address2; }
    }
}

Popular Answer

Are you using this for "Read-Only". How would you expect your ORM to write the data back to the database ?

If it is read-only, instead of querying the table directly, you could wrap it in a view.

CREATE VIEW DepartmentView
(
    SELECT Address1 + ', ' + Address2 as Address FROM Department
)

and then just map the view instead.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why