检查对象的类型是 IQueryable 还是 IEnumerable

To check if an object is of type IQueryable or IEnumerable

我有这个声明:

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

它被声明为 IEnumerable<Person> 但它是用 IQueryable<Person> 实例化的。我了解 IQueryable<T> 继承自 IEnumerable<T>

为了可重用性,我创建了一个接受 IEnumerable 的方法,而不是分别接受 IEnumerableIQueryable 的两个方法:

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;
}

在消费点,我有:

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

对于内存中的集合,我有:

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

GetPersonInfo 方法中,我如何检查 persons 是否为 IQueryable

很容易成为解决方案,但如果我正确理解你的问题..

if (persons is IQueryable)