IQueryable(非通用):缺少计数和跳过?它与 IQueryable一起使用

发布于 2024-10-04 04:30:31 字数 1375 浏览 7 评论 0原文

我有一个扩展方法,有人给了我非常有帮助...它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月蹉跎了容颜 2024-10-11 04:30:31

嗯,很简单,这些方法不适用于 IQueryable。如果您查看可查询方法,您会发现会发现它们几乎全部基于 IQueryable

如果您的数据源在执行时确实IQueryable,而您只是不知道T是什么,那么您可以通过反射发现...或者在 C# 4 中,只需使用动态类型:

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执行时间。

但总的来说,我鼓励您尝试在任何地方都使用通用形式 - 它可能会简单得多。

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>.

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

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.

小兔几 2024-10-11 04:30:31

这个问题很老了,但是当有人需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文