none
Selecting columns in Entity Framework Query RRS feed

  • Question

  • Currently (for example) I have a syntax written like this.

    context.applications.OrderBy(p => p.app_code).Skip(pageSize * pageNo).Take(pageSize).ToList();

    How can I select all columns from the main table and few columns from other related tables in EF query. Like "SELECT applications.*, applicationtype.description, ...."


    Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH) (skype: ali.net.pk)

    Thursday, April 5, 2012 12:35 PM

Answers

  • Hi Ali.NET,

    You can use the .Select() method to project into an anonymous type or a defined non-entity class:

    context.applications.OrderBy(p => p.app_code).Skip(pageSize * pageNo).Take(pageSize)
        .Select(p => new {
                Application = p,
                TypeDescription = p.ApplicationType.Description,
            }).ToList()

    See the link for more info: http://msdn.microsoft.com/en-us/library/bb896275.aspx

    Regards,

    Tyler

    • Marked as answer by Khadim Ali Thursday, April 5, 2012 1:16 PM
    Thursday, April 5, 2012 12:51 PM