2011-04-07 99 views
1

我已经建立了一个分页类,对于一个特定的数据类型, 效果很好,但现在我需要做动态是否可以创建匿名类型泛型?

这里的类型是我的代码

public class Pagination { 
    public IQueryable<Character> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages { 
     get { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<Character> items, int pageSize) { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<Character> GetPage(int pageNumber) { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 

,你可以看到,这个分页类仅适用于'Character',是否可以创建匿名数据类型并调用Skip和Take等通用方法?

+0

看起来像很多人在同一时间注意到这一点。 – 2011-04-07 21:58:51

+0

哇,没错。你的答案大部分是有效的!非常感谢,但我只能打勾1 ... – walter 2011-04-07 22:15:39

回答

5

是的,你可以创建班级为通用类:

public class Pagination<T> { 
    public IQueryable<T> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages { 
     get { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<T> items, int pageSize) { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<T> GetPage(int pageNumber) { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 
+0

应该有资格“'在哪里T:class'” – 2011-04-07 21:59:49

+0

@ScottSEA为什么?我不认为这种约束是绝对必要的。 – 2011-04-07 22:02:45

+0

不,它不需要它。 – Aliostad 2011-04-07 22:06:06

3

您只需将泛型用于基类并继承并实现该类。

public class Pagination <T> where T : class 

然后把吨您目前拥有的字符类的所有地方

1

这应该是很简单...... 试试这个...

public class Pagination<_type> 
{ 
    public IQueryable<_type> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages { 
     get { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<_type> items, int pageSize) { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<_type> GetPage(int pageNumber) { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 

你可能要添加约束_type,就像这样(它使_type必须是一个类):

public class Pagination<_type> where _type : class 
0

确定它是pos sible,但它通常只在方法体内有用。例如

var array = new[] { 
    new { Name = "Jared", Age = 30 }, 
    new { Name = "Bob", Age = 21 } 
}; 
var query = array.Skip(1).Take(1); 

这成为问题的方法之间传递,但因为没有办法说出一个匿名类型的名称,因此不出可以为TIEnumerable<T>。这可能与通用技巧有关,但它似乎并不适合您在此场景中需要的内容。

1

此通用类应该完成这项工作。

public class Pagination<T> 
{ 
    public IQueryable<T> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages 
    { 
     get 
     { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<T> items, int pageSize) 
    { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<T> GetPage(int pageNumber) 
    { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 
0

将这项工作吗?

public class Pagination<T> { 
    public IQueryable<T> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages { 
     get { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<T> items, int pageSize) { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<T> GetPage(int pageNumber) { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 

我不确定我看到与匿名类型的关联?为什么你需要这种类型以适用于Character以外的类型?

0

你试过吗?

public class Pagination<T> where T : class 
{ 
    public IQueryable<T> Items { get; set; } 

    public int PageSize { get; set; } 

    public int TotalPages { 
     get { 
      if (this.Items.Count() % this.PageSize == 0) 
       return this.Items.Count()/this.PageSize; 
      else 
       return (this.Items.Count()/this.PageSize) + 1; 
     } 
    } 

    public Pagination(IQueryable<T> items, int pageSize) { 
     this.PageSize = pageSize; 
     this.Items = items; 
    } 

    public IQueryable<T> GetPage(int pageNumber) { 
     pageNumber = pageNumber - 1; 
     return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
    } 
} 
1

是的,这是可能的。我会做下列方式:

  1. 隐藏的实现中,仅公开接口:

    public interface IPagination<T> 
    { 
        int PageSize { get; set; } 
        int TotalPages { get; } 
        IQueryable<T> GetPage(int page); 
    } 
    
  2. 充分利用Pagination类通用的,只是一个内部实现(外面只能看到接口)

    public class Pagination<T> : IPagination<T> { 
        public IQueryable<T> Items { get; set; } 
    
        public int PageSize { get; set; } 
    
        public int TotalPages { 
         get { 
            if (this.Items.Count() % this.PageSize == 0) 
            return this.Items.Count()/this.PageSize; 
           else 
            return (this.Items.Count()/this.PageSize) + 1; 
          } 
         } 
    
         internal Pagination(IQueryable<T> items, int pageSize) { 
          this.PageSize = pageSize; 
          this.Items = items; 
         } 
    
         public IQueryable<T> GetPage(int pageNumber) { 
          pageNumber = pageNumber - 1; 
          return this.Items.Skip(this.PageSize * pageNumber).Take(this.PageSize); 
         } 
    } 
    
  3. 提供一种扩展方法来转换:

    public static IPagination<T> AsPagination (this IQueryable<T> source, int pageSize) 
    { 
        if(source == null) 
         throw new ArgumentNullException("source"); 
        return new Pagination<T>(source, pageSize); 
    }