这是作弊,但也许在您的上下文中是有意义的:
interface IReferencedQueryable<T> : IQueryable<T>
{
IEnumerable<T> Source { get; }
}
static class IReferencedQueryableExtensions
{
public static IReferencedQueryable<T> AsReferencedQueryable<T>(
this IEnumerable<T> source)
{
return ReferencedQueryable.From(source);
}
class ReferencedQueryable<T> : IReferencedQueryable<T>
{
public IEnumerable<T> Source { get; private set; }
ReferencedQueryable(IEnumerable<T> source)
{
Source = source;
}
static IReferencedQueryable<T> From(IEnumerable<T> source)
{
return new ReferencedQueryable(source);
}
// all the IQueryable members would be
// implemented through AsQueryable()
// ...
}
public static IReferencedQueryable<T> Where<T>(
this IReferencedQueryable<T> source,
Expression<Func<T, bool>> predicate)
{
return ReferencedQueryable.From(
((IQueryable<T>)source).Where(predicate));
}
// do the same for all the Linq extension methos you want to support
}
你会像这样使用它:
T[] itemArray = // initialized values
var itemQuery = itemArray.AsReferencedQueryable()
.Where(*/some query*/)
.Skip(5)
.Etc() ...
当您需要您的源序列时,您可以通过Source
成员访问它,可能通过as
演员表。
var s = itemQuery.Source;