2012-08-31 86 views
0

我正在嘗試使用post:How do you sort an EntitySet<T>中的答案來公開接口,以便我可以使用綁定列表對EntitySet進行排序。我創建了下面的類,並且我得到了下面的編譯器錯誤:「無法找到類型或名稱空間'P'(你是否缺少使用指令或程序集引用?)有人可以告訴我P的含義和哪個名稱空間我需要包含以下方法來編譯?我對代表和lamba表達式很新。P在C#表達式中表示什麼?

另外,有人可以確認,如果我從我的EntitySet創建一個BindingList,我對BindingList所做的任何修改將是到EntitySet的製作?

基本上,我有一個EntitySet的,我需要整理和更改。然後,我將需要堅持使用原來的實體中的BindingList是從哪裏來的這些變化。

public class EntitySetBindingWrapper<T> : BindingList<T> 
{ 
    public EntitySetBindingWrapper(BindingList<T> root) 
     : base(root) 
    { 
    } 

      public void Sort<P>(Expression<Func<T, P>> expr, ListSortDirection direction) 
    { 
     if (expr == null) 
      base.RemoveSortCore(); 

     MemberExpression propExpr = expr as MemberExpression; 
     if (propExpr == null) throw new ArgumentException("You must provide a property", "expr"); 

     PropertyDescriptorCollection descriptorCol = TypeDescriptor.GetProperties(typeof(T)); 
     IEnumerable<PropertyDescriptor> descriptors = descriptorCol.Cast<PropertyDescriptor>(); 
     PropertyDescriptor descriptor = descriptors.First(pd => pd.Name == propExpr.Member.Name); 

     base.ApplySortCore(descriptor, direction); 
    } 
} 

我終於得到了上面的代碼編譯,但現在我得到一個錯誤,當我嘗試調用構造函數:

下面的代碼,其中currentPredefinedJob.fkItems是EntitySet的結果錯誤:無法從轉換System.ComponentModel.IBindingList到System.ComponentModel.BindingList

var bindingWrapper = new EntitySetBindingWrapper<PredefinedJobsItem>(currentPredefinedJob.fkItems.GetNewBindingList()); 

而且,下面的代碼導致錯誤:錯誤8使用泛型類型 'MarineService.Tests.EntitySetBindingWrapper' 要求 '1' 類型參數

var bindingWrapper = new EntitySetBindingWrapper(currentPredefinedJob.fkItems.GetNewBindingList()); 

有人可以告訴我我需要怎樣調用這個構造函數,並確認如何對產生的BindingList進行排序?

回答

1

您需要在類定義或方法定義中指定通用變量。

P將類型由你的函數expr

public void Sort<P>(Expression<Func<T, P>> expr, ListSortDirection direction) 
+0

好的,我想我理解關於P是返回類型的部分。但是,我甚至無法讓類在方法簽名中用P編譯。有沒有我失蹤的名稱空間?到目前爲止,我已經包括:System.Linq.Expressions,System.Linq.Data和System.Linq。 – Grasshopper

+0

@Grasshopper - 你得到了什麼錯誤信息? – Aducci

+0

一旦我清理了項目並重新編譯後,我能夠解決這個錯誤,但是現在當我嘗試實例化列表時,我又獲得了另一次。你介意看看我的代碼示例和修改後的相應錯誤嗎? – Grasshopper

0

返回調用構造函數,以下工作正常:

var w = new EntitySetBindingWrapper<String>(new System.ComponentModel.BindingList<string>());

是否有可能問題出在什麼你來正在做currentPredefinedJob.fkItems.GetNewBindingList()

相關問題