To create and assign classes on the fly from other objects I usually use LINQ.
Suppose I have a IQueryable<ProductForCart>
object fetched from my Database and, for some reason, I need to create an IQuaryable<Product>
where each Product
properties are extracted from the ProductForCart
.
I simply do:
IQueryable<ProductForCart> productsForCart = ProductRepository.GetProductsForCart();
IQueryable<Product> products = productsForCart.Select(sc => new Product
{
ProdId = sc.ProdId,
ProdName = sc.ProdName,
//other properties
}
This works smooth. But if I have IList<ProductForCart>
and IList<Product>
it throws a runtime exception:
This function can only be invoked from LINQ to Entities.
I didn't know that LinqToObjects did not support this kind of assignments. Or am I missing something?