IQueryable(非通用):缺少计数和跳过?它与IQueryable< T>一起使用 [英] IQueryable (non generic) : missing Count and Skip ? it works with IQueryable<T>

查看:78
本文介绍了IQueryable(非通用):缺少计数和跳过?它与IQueryable< T>一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展方法,一个人真的很乐于给我...它在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)

这里是代码,count和Skip和我认为Take丢失了.

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,因此我的非Generic 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<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>.

如果您的数据源在执行时真正成为IQueryable<T>,而您只是不知道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(非通用):缺少计数和跳过?它与IQueryable&lt; T&gt;一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆