IQueryable(非通用):缺少计数和跳过?它与 IQueryable一起使用
我有一个扩展方法,有人给了我非常有帮助...它在 IQueryable 上执行 orderby ...但我想要一个执行正常的 IQueryable (非通用)
这是代码,计数和跳过和我觉得拿都不见了。
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
}
这是并且它运行完美,没有错误。
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 上运行。
有解决方法吗?
预先感谢
编辑
我在现有的 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 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);
}
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 ..
Is there a work around?
Thanks in advance
EDIT
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,很简单,这些方法不适用于 IQueryable。如果您查看
可查询
方法,您会发现会发现它们几乎全部基于IQueryable
。如果您的数据源在执行时确实是
IQueryable
,而您只是不知道T
是什么,那么您可以通过反射发现...或者在 C# 4 中,只需使用动态类型:C# 编译器的动态位将在以下位置为您计算
T
执行时间。但总的来说,我鼓励您尝试在任何地方都使用通用形式 - 它可能会简单得多。
Well, quite simply those methods aren't available for
IQueryable
. If you look at theQueryable
methods you'll see they're almost all based onIQueryable<T>
.If your data source will really be an
IQueryable<T>
at execution time, and you just don't know whatT
is, then you could find that out with reflection... or in C# 4, just use dynamic typing: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 扩展方法时,他/她应该在返回类型为匿名时返回 IQueriable。
The question is old but when someone needs the IQueryable extension methods then he/she should return IQueriable when the return type is anonymous.