Yar
Yar

Reputation: 7447

Passing a generic parameter into a function which has a specific inheritance

I have this function doing queries based on a customerId and entityModel. I want to make it generic for all different models. In other words, I want to pass CustomerFeatureEntity class as a parameter into this function:

public TableQuery<CustomerFeatureEntity> StorageQuery(string customerId)
{
    TableQuery<CustomerFeatureEntity> query = new TableQuery<CustomerFeatureEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, customerId));
}

Also it would be nice if I make sure that parameter inheritance from TableEntity class. Is either one of those possible to be done in C#?

Upvotes: 0

Views: 33

Answers (1)

David L
David L

Reputation: 33815

Yes, you'd just need to redefine it to use generics with a where constraint.

public TableQuery<T> StorageQuery<T>(string customerId) where T : TableEntity
{
    TableQuery<T> query = new TableQuery<T>()
        .Where(TableQuery.GenerateFilterCondition("PartitionKey", 
            QueryComparisons.Equal, customerId));
}

Upvotes: 1

Related Questions