I’m using Linq to select items from the database and Entity Frameworks to create my entities. Entity Frameworks has also created Complex Types using the Import Function Wizard. Each time I use any of the Complex Types to return a query I get a null value when I attempt to cast the result as the Complex Return Type.
private AWorksLTEntities db = new AWorksLTEntities();
public IQueryable<SelectAll_Result> SelectAllMethod()
{
var result = from p in db.Products
select p;
return result as IQueryable<SelectAll_Result>;
}
SelectAll_Result is the complex type created by EntityFrameworks and the cast does not work and my method returns null.
The same code using the normal entities does return values
private AWorksLTEntities db = new AWorksLTEntities();
public Product SelectAllMethod()
{
var result = from p in db.Products
select p;
return result;
}
My problem is that whilst this example is very simple, I am creating this queries using a code generator I am working on, and the return type is difficult to ascertain, so I will need to be able to use the complex return types generated by entity Frameworks Function Imports rather than the standard entity returns.
Any ideas? Thanks in advance.
The cast operators do not know how to convert from one class to another one that is unrelated. You have to provide the conversion yourself: