Tony_KiloPapaMikeGolf
Tony_KiloPapaMikeGolf

Reputation: 889

C# , make method more generic

Please look at the following method:

internal IEnumerable<Query> FilterOnUserInvolvement(IEnumerable<Query> input)
{
    var user = _userManager.GetUserByADName(_user.Identity.Name);
    if (_userManager.IsUserAdministrator(user) || _userManager.IsUserStaff(user))
    {
        return input;
    }
    else
    {
        using (var context = new QAContext())
        {
            var involvedQueries = context.UserInvolvement.Where(x => x.UserID == user.ID).Select(x => x.QueryID).ToList();
            return input.Where(i => involvedQueries.Contains(i.ID));
        }
    }
}

Now this method takes IEnumerable<Query> and returns the same.

Actually the functionality of this method could be applied to any IEnumerable<Type> that holds ID.

How can I rewrite this query, using generics so it can be called with another Type that holds ID?

Upvotes: 0

Views: 112

Answers (1)

maccettura
maccettura

Reputation: 10818

Just restrict your T to a type that contains ID:

internal IEnumerable<T> FilterOnUserInvolvement<T>(IEnumerable<T> input) where T : ISomeInterfaceWithId
{

}

Upvotes: 6

Related Questions