Entity Framework LINQ query RRS feed

  • Question

  • Hi Everyone,

    I have a quick question and was hoping someone can help.  I have two table that have a 1 to many relationship. I  wrote the below LINQ query to obtain the description column from the primary table since the ID won't make sense to a user in a gridview. My question is, how can I used the .Include() function in a LINQ query to obtain just the one  column from the "CostCenterType" table similar to what I did on the below table. Is this possible, just curious since I'm learning. Thanks in advance..

    var query = (from c in DbContext.CostCenters select new { c.ID, c.CostCenterNumber, c.CostCenterName,

    c.CostCenterType_ID, c.CostCenterType.NameType, c.UpdateBy, c.UpdateDateTime,

    c.RowVersion }).ToList(); RadGrid1.DataSource = query;

    • Edited by zXSwordXz Sunday, August 24, 2014 4:41 PM
    Sunday, August 24, 2014 4:41 PM

All replies

  • We have two approach in Entity framework, Model first and code first approach . In the model first the parent entity(CostCenterType) reference available to the child entity(CostCenters) .So, you could able see parent entity data will be available in the child entity. Suppose, If you want load your parent entity to the child entity, try this below

     var query = (from c in DbContext.CostCenters.Include("CostCenterType")
                             select new

    Wednesday, January 7, 2015 6:08 AM