What is the best way to change values of fields in a query? RRS feed

  • Question

  • Hi,

      Consider that I have Invoices and Items tables in the entity struture. This function provides the data:

      public IQueryable<Invoices> Query()
         return Context().Invoices.Include("Items");

      No problem so far. Now I need to change the value of one of the Invoices table's field.

      If I include .Select(s => new { Total = s.Total - s.Discount }) I get the desired value but I lose all other fields.

      What is the easiet way to solve this? I need to create a POCO and populate field by field? Or there is a better way?


      Marco Castro

    Friday, December 30, 2011 1:01 PM


  • Hi Marco,

    You can't change the values of the fields of an Entity as you're loading it from the DB. So that would leave you with two options.

    1. Populate an in-memory collection and then iterate over it to change the fields you want before returning the collection.
    2. Project the query into a POCO/DTO.



    Friday, December 30, 2011 1:59 PM