I would like to create a reusable method that I can use in Select() projections. Here is a simple example:
public partial class Product
{
public IQueryable<ProductImage> GetProductImages(bool ActiveOnly)
{
return this.ProductImages.Where(i => !ActiveOnly || i.IsActive);
}
}
public class App
{
public void Main()
{
var product = db.Products.Select(p => new {
p.Name,
p.GetProductImages(true)
}
}
}
The above will fail with the standard LINQ to Entities does not recognize the method ...
and I get that I can call db.Products.ToList().Select(...)
, but that is not what I am looking for, as in my case, I am projecting in order to avoid pulling unnecessary data into memory.
I have successfully been using Microsoft.Linq.Translations for custom properties for a while now, however that will not work in my example because I need to pass in a parameter, so a method is really the only way I can see this working. Any help is greatly appreciated.