Generate DTO from entity framework RRS feed

  • Question

  • Suppose I use Entity Framework 4.0, how to generate data transfer object manually?

    Need example with C#.

    Thursday, April 25, 2013 3:06 PM


  • Hi ardore;

    To create a Data Transfer Object / DTO, all you need to do is to create a new class which have public accessors for the properties you wish to publish. You can wish to fill a collection of these object by using it in the Select clause of the Linq to EF query. For example lets say you query the Customer table which has 10 columns but you only need three columns to be sent to the receiver of this collection then you can do this. 

    // DTO class
    public class MyDTO
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string PhoneNo { get; set; }
    List<MyDTO> customerDTO = (from c in db.Customer
                               select new MyDTO()
                                   CompanyName = c.CompanyName,
                                   ContactName = c.Contact,
                                   PhoneNo = c.Phone

    That is it.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by ardmore Thursday, April 25, 2013 5:34 PM
    Thursday, April 25, 2013 4:44 PM