2011-04-07 97 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); 
    }