IQueryable(非通用):缺少 Count 和 Skip ?它适用于 IQueryable<T> [英] IQueryable (non generic) : missing Count and Skip ? it works with IQueryable<T>
问题描述
我有一个扩展方法,有人给我提供了非常有用的方法...它在 IQueryable 上执行 orderby ...但我想要一个普通的 IQueryable(非通用)
i have an extension method which a person was really helpful to give me... it does an orderby on IQueryable ... but i wanted one to do a normal IQueryable (non generic)
这是代码,计数和跳过,我认为缺少.
Here is the code, The count and Skip and i think Take are missing .
public static IQueryable GetPage(this IQueryable query,
int page, int pageSize, out int count)
{
int skip = (int)((page - 1) * pageSize);
count = query.Count(); //COUNT DOESN'T EXIST
return query.Skip(skip).Take((int)pageSize); // NEITHER SKIP
}
这是它,它完全没有错误.
Here is the and it works perfectly no errors.
public static IQueryable<T> GetPage<T>(this IQueryable<T> query,
int page, int pageSize, out int count)
{
int skip = (int)((page - 1) * pageSize);
count = query.Count();
return query.Skip(skip).Take((int)pageSize);
}
有什么想法可以解决这个问题吗?我不想更改我的返回类型,因为它可以完美运行,而且我有另一个名为 ToDataTable 的扩展方法,它也可以在非通用 IQueryable 上运行..
Any ideas how i can get around this? I don't want to change my return types as it works perfectly and i have another extension method called ToDataTable and it also functions on a non generic IQueryable ..
有解决办法吗?
提前致谢
编辑
我在现有的 IQueryable 上这样称呼它
I call it like so on an existing IQueryable
IQueryable<Client> gen = null;
IQueryable nongen = null;
var test = gen.GetPage(); //COMPILES!
var test 1 = non.GetPage(); // Doesn't compile because GETPAGE
// for non generic is broken as it has
// invalid methods like COUNT and SKIP etc.
我尝试删除 GetPage 非通用版本,但随后我的非通用 Iqueryable 没有选择扩展名,因为它不是 Iqueryable 而只是 IQueryable
I tried removing the GetPage non generic version but then my non Generic Iqueryable doesn't pickup the extension due to the fact its not a Iqueryable but only an IQueryable
推荐答案
嗯,很简单,那些方法不适用于 IQueryable
.如果您查看 Queryable
方法,您将看到它们几乎都基于 IQueryable
.
Well, quite simply those methods aren't available for IQueryable
. If you look at the Queryable
methods you'll see they're almost all based on IQueryable<T>
.
如果你的数据源在执行时真的是一个IQueryable
,而你只是不知道T
是什么,然后您可以通过反射找到它...或者在 C# 4 中,只需使用动态类型:
If your data source will really be an IQueryable<T>
at execution time, and you just don't know what T
is, then you could find that out with reflection... or in C# 4, just use dynamic typing:
public static IQueryable GetPage(this IQueryable query,
int page, int pageSize, out int count)
{
int skip = (int)((page - 1) * pageSize);
dynamic dynamicQuery = query;
count = Queryable.Count(dynamicQuery);
return Queryable.Take(Queryable.Skip(dynamicQuery, skip), pageSize);
}
C# 编译器的动态位会在执行时为您处理T
.
The dynamic bit of the C# compiler will take care of working out T
for you at execution time.
不过,总的来说,我鼓励您尝试在任何地方使用通用形式 - 它可能会简单得多.
In general though, I'd encourage you to just try to use the generic form everywhere instead - it's likely to be significantly simpler.
这篇关于IQueryable(非通用):缺少 Count 和 Skip ?它适用于 IQueryable<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!