2013-01-24 92 views
1

我的程序應該讀取和處理來自傳統平面文件的數據。DataBind to ICollection <T>

我將數據讀入private ICollection<transaction> transactions;

public class transaction 
{ 
    public int transactionNum; 
    public DateTime transactionDate; 
    public int orderNum; 
    public string customerName; 
    public Decimal amount; 
} 

如何創建一個DataSourceICollection<T>(或它的LINQ查詢),讓我可以DataBind幾個控制值呢?

注:我有最小的LINQ知識。在過去,我可以將一個數據庫表拖入我的應用程序中。

+0

DataSource for what?你使用的是ASP.NET嗎? –

+0

我不明白這個問題,你可以將它綁定到winforms或ASP.NET中的控件。它只需要實現'ICollection'。 http://msdn.microsoft.com/en-us/library/system.web.ui.idatasource.getviewnames.aspx –

+0

在ASP.NET窗體上,我有一個名爲'customerCheckL'的CheckBoxList。在加載數據文件時,代碼應該在'customerCheckL.Items'中填充不同客戶名稱的列表。我怎麼做? 'customerCheckL.DataSource = ???' – Steven

回答

1

從您的評論:

在ASP.NET表單,我有一個名爲customerCheckL CheckBoxList的。在 加載數據文件時,代碼應該使用不同客戶名稱的列表填充customerCheckL.Items 。我怎麼做? customerCheckL.DataSourceID = ???

這樣做更有意義。你可以實現一個EqulityComparer<transactions>類的客戶比較:

public class TransactionCustomerComparer : IEqualityComparer<transaction> 
{ 
    public bool Equals(transaction x, transaction y) 
    { 
     if (x == null || y == null) return false; 
     return x.customerName == y.customerName; 
    } 

    public int GetHashCode(transaction obj) 
    { 
     if (obj == null) return int.MinValue; 
     return obj.customerName.GetHashCode(); 
    } 
} 

(請注意,您可以在所有Enumerable方法,允許通過自定義比較使用此方法)

然後你可以使用Distinct獲得獨特的名單。您只需設置DataSource,DataTextFieldDataValueFieldDataBindCheckBoxList即可。

var customerComparer = new TransactionCustomerComparer(); 
customerCheckL.DataSource = transactions.Distinct(customerComparer); 
customerCheckL.DataTextField = "customerName"; 
customerCheckL.DataValueField = "transactionNum"; 
customerCheckL.DataBind(); 
相關問題