views:

28

answers:

1

i want to create a generic class that would accept T.

T is an object coming from Entity Framework representing a Table, or a View of that table.

properties on both would be the same.

I would like to create a generic class, that will accept the table or view, and construct a linq query based on the properties.

so i would need to do something like.. class Foo Where T : myTable or T : myView

so that later i could use the strongly typed properties to construct my predicates.

How could i achieve something like this?

the way i construct my query looks something like this:

        if (critera.IsTradeDate)
            predicate = PredicateUtility.And(predicate, t => t.DateTrade >= critera.FromDate);

the t is the type that needs to be strong, and properties used will be the same on table and view. So in this case, t should represent either my table or my view, to re-use the code, but still leverage entity framework..

+2  A: 

Create an interface ITableOrView (or some better name). Apply the interface to both partial classes for your Entities that are the Table or View.

Create your generic class with where T : ITableOrView

Now you can use the interface types.

BUT you can't use interfaces in many places in Entity Framework queries so you'll actually need to delegate that work back to the 'T' class itself.

Hightechrider
yep, that's a good idea. however my table and view objects are generated by EF.. so i don't have direct control over them. i could add partial classes to implement that interface, but thought i'd ask if there is any other slick way to achieve it (maybe using constraints but not necessarily)
Sonic Soul