Vinyl Windows
Vinyl Windows

Reputation: 503

How to work with generic <T> with EntitySet<T>

I have a opteration that needs to run on three types of EntitySets; however, the data for each type is the same. Linq-to-SQL is creating these three types to match up with a few between, "tween" tables in my database.

Is there a way to work with generic EntitySet types like I've got them below?

private EntitySet<T> GetClientHorizontal(EntitySet<T> clientHorizontal) {}

It is to solve the redundant issue like below.

private EntitySet<LeafHorizontal> 
   GetClientLeafHorizontal(EntitySet<LeafHorizontal> clientLeafHorizontal) { }

private EntitySet<BayHorizontal>
   GetClientBayHorizontal(EntitySet<BayHorizontal> clientBayHorizontal) { }

private EntitySet<SideliteHorizontal>
   GetClientSideliteHorizontal(
      EntitySet<SideliteHorizontal> clientSideliteHorizontal) { }

Upvotes: 1

Views: 569

Answers (1)

Pavel Morshenyuk
Pavel Morshenyuk

Reputation: 11471

If you are saying that these three classes are almost the same - then you can create an interface or base class with common members:

interface IHorizontal
{
  int SuperValue { get; set; }
}

Then you need to inherit LeafHorizontal, BayHorizontal and SideliteHorizontal classes from this interface:

public partial class LeafHorizontal : IHorizontal { ... }
public partial class BayHorizontal : IHorizontal { ... }
public partial class SideliteHorizontal : IHorizontal { ... }

After that you can create generic method:

private EntitySet<T> GetClientBayHorizontal(EntitySet<T> clientBayHorizontal)  where T : IHorizontal 
{
    clientBayHorizontal.SuperValue++;
}

Upvotes: 2

Related Questions