Mazaher Bazari
Mazaher Bazari

Reputation: 421

To check if an object is of type IQueryable or IEnumerable

I have this statement:

IEnumerable<Person> persons= context.Persons.Where(/*SomeCondition*/);

It's declared as IEnumerable<Person> but it's instantiated with IQueryable<Person>. I understand IQueryable<T> inherits from IEnumerable<T>.

For re-usability, I create a method that accept IEnumerable instead of two methods that accept IEnumerable and IQueryable respectively:

public IEnumerable<PersonInfo> GetPersonInfo(IEnumerable<Person> persons)
{ 
    persons = persons.Where(/*Some condition*/);
    IEnumerable<Person>  persons = persons.GroupBy(/*based on some property*/);

    /*Here i want check value type of persons is IQueryable or not*/

    return persons;
}

At the point of consumption, I have:

IQueryable<Person> persons= context.Persons.Where(/*condition*/);
IQueryable<PersonInfo> result GetPersonInfo(persons.AsEnumerable()).AsQueryable();

And for In-Memory Collection I have:

List<Person> persons = new List<Person> { new Person {}};
IEnumerable<Person> result = GetPersonInfo(persons);

At the GetPersonInfo method, how can i check persons is IQueryable or not?

Upvotes: 1

Views: 1348

Answers (1)

Lamelas84
Lamelas84

Reputation: 992

So easy to be the solution, but if I have understand your question correctly..

if (persons is IQueryable)

Upvotes: 4

Related Questions