0

I hope my wording makes sense... I wasn't quite sure exactly how to explain what I'm looking to do.

I have a method in a generic class that returns a list of entities as follows:

public abstract class ChildCRUDController<TModel> : CRUDController<TModel, ... >
    where TModel : IChildEntity

public ViewResult List(int id)
{
    return View(repository.GetMany(x => x.ParentID == id));
}

This controller is implemented by quite a few other controllers. The issue I have is that not all entities that implement IChildEntity have the same parent type. To get around this issue I created ParentID properties for all the models that implement IChildEntity so they could use the same controller.

public partial class PhoneNumber : IChildEntity
{
    public int ParentID
    {
        get { return CustomerID; }
        set { CustomerID = ParentID; }
    }
}

and...

public partial class Transaction : IChildEntity
{
    public int ParentID
    {
        get { return LeaseID; }
        set { LeaseID= ParentID; }
    }
}

But when I call the List method above I get the following error:

The specified type member 'ParentID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Is there any way I can achieve the result I am looking for without pulling the object set into memory or renaming all the properties on the entities themselves?

Thanks!

Jeff Camera
  • 5,324
  • 5
  • 44
  • 59

1 Answers1

1

If you are willing to pass the field name into the List method and to construct your own query you can do it using the techniques described in this StackOverflow article:

Or you could supply the ChildCRUDController with another generic type parameter constrained to an interface that supplies the field name and again use it dynamically.

Community
  • 1
  • 1
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95