Bevin
Bevin

Reputation: 173

IEnumerable<T> generics - "Cannot find symbol" inside method (T is passed by the calling function)

I'm trying to use IEnumerable generics. Im passing the data type from another file while calling the corresponding method. But when using were to filter the IEnumerable i get the error "symbol not defined" since the IEnumerable method is passed during run time.

public IEnumerable<T> FetchData<T>(int take, int skip, string guidRelatedA, string guidRelatedB, bool filterA, bool filterB, IEnumerable<T> dataToFilter)
{
    if (filterA && filterB)
    {
        var  query = from p in dataToFilter
                     .Where(o => (o.FilterA.Any(f => f.Id.ToString().Equals(guidRelatedA))) &&
                                 (o.FilterB.Any(f => f.Id.ToString().Equals(guidRelatedB))))
                     .Take(take)
                     .Skip(skip)
                     select p;
        return query;
     } 
}

FilterA and FilterB throws me an error,FilterA and FilterB is common to all the classes im using, Is there any workaround to overcome this?

Upvotes: 0

Views: 137

Answers (1)

Will
Will

Reputation: 928

It sounds like you need to add a constraint for T where T : base class or interface.

So something like

public interface IFilterable 
{
   object FilterA {get; set;}
   object FilterB {get; set;}
}

public IEnumerable<T> FetchData<T>(int take, int skip, string guidRelatedA, string guidRelatedB, bool filterA,
                             bool filterB, IEnumerable<T> dataToFilter) where T : IFilterable
{
...
}

Upvotes: 4

Related Questions