Search code examples
c#entity-frameworklinq-to-entities

Add an additional property when querying database using Linq to Entities


So I'm using Repository pattern to take all record from the database :

var rows = DocumentsRepository.All();

However I want to add one additional property. Documents table has column Date of type datetime and I want to keep the date in separate property. Something like :

new {DocumentDate = d.Date.Date}

It's just an example to get bette idea of what I want to accomplish. I just guess that I'm gonna need anonymous objects for this job. But not sure how to implement it and also - just to be as clear as possible - I want to be able to use all my other properties and this one along with those that are originally part of the entity.


Solution

  • There is no need to transfer more data over the network. You already have Date, so you can just define DocumentDate property on your document entity class with getter only (it will not be persisted):

       public DateTime DocumentDate 
       { 
           get { return Date.Date; } 
       }