I have 2 tables Products and ProductDetails. Products contains all information about my stock and ProductDetails is used to allow product price changes to be updated in the system in advance of when they come into action.
I am trying to write what should be a simple query that returns all products and also updates the Cost and Price of the product if a matching row is found in ProductDetails where the CommenceDate is less than or equal to the current date. What I have so far is:
var products = from p in Products
from pd in ProductDetails
.Where(pd => pd.ProductID == p.ID && pd.CommenceDate <= DateTime.Now)
.DefaultIfEmpty()
where p.InUse == true
select p;
I first thought that I could achieve what I was after by declaring a new product in the select e.g:
var products = from p in Products
from pd in ProductDetails
.Where(pd => pd.ProductID == p.ID && pd.CommenceDate <= DateTime.Now)
.DefaultIfEmpty()
where p.InUse == true
select new Product {
ID = p.ID,
Description = p.Description,
....
Cost = pd.Cost.HasValue ? pd.Cost : p.Cost,
Price = pd.Price.HasValue ? pd.Price : p.Price,
...
};
This code gives me the following error:
Explicit construction of entity type 'LINQPad.User.Product' in query is not allowed
Is there a simple way to say 'return product and optionally replace some property values'?