I have two class that map two tables of my database:
public class Product
{
public int Id { get; set; }
public string Token { get; set; }
public string Name { get; set; }
public decimal Value { get; set; }
}
public class Ticket
{
public int Id { get; set; }
public string SerialNumber { get; set; }
public string ProductToken { get; set; }
public Product Product { get; set; }
}
For some domain reasons, Product and Ticket are logically linked, in other words, they are not linked in a database relationship that could be mapped by EF, they will be linked in my app with a linq query that "must" be translated in a SQL Outer Left Join. From this, i did the following query:
IQueryble<Ticket> query = from ts in context.Tickets
join ps in context.Products
on ts.ProductToken equals ps.Token into p
select new Ticket
{
Id = t.Id,
SerialNumber = t.SerialNumber,
ProductToken = t.ProductToken,
Goal -----> Product = p.FirstOrDefault()
};
The query keeps as IQueryble because after that, the query keeps to be refined with a filter.
The problem is when i run the following code:
var tickets = query.OrderBy(t => t.SerialNumber).ToList();
I got the following error:
"The entity or complex type 'Model.Ticket' cannot be constructed in a LINQ to Entities query."
So, how can i reach my goal?