2011-08-12 60 views
1

以下link建議使用此語法:SortedList錯誤:「不包含Select的定義。」

var result = SortedList.Select(x => new { 
    x.Value.MyProperty, 
    x.Value.AnotherProperty 
}); 

然而,當我嘗試測試它,我收到錯誤:

System.Collections.SortedList does not contain a definition for Select .

我已經有以下引用:

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.IO; 

儘管廣泛的互聯網搜索,我無法弄清楚如何解決這個問題。誰能幫忙?

編輯: 也許我正在談論這個錯誤。我的目標是將SortedList的子集作爲SortedList返回。

var subset = data.Where(x => x.Key >= startDate && x.Key <= endDate); 

其中 '數據' 是

SortedList<DateTime, BarData> 

'barData' 是一類

+2

請確保您有System.Core.dll除此之外,「排序列表」需要有一個實例變量 - 您使用的不知道,因爲「排序列表」是你的榜樣帕斯卡情況。 –

+0

您是否嘗試過使用System.Collections.Generic.SortedList? – snurre

+0

我得到了一個自定義詞典的「不包含Select的定義」的錯誤。我從來沒有找到錯誤的直接原因,因爲我擁有所有正確的名稱空間和dll。但是,當我停止使用自己的自定義KeyValuePair類並使用標準的KeyValuePair類時,問題就消失了。我懷疑這個問題與我的類實現IEnumerable 和IEnumerable >有關。 –

回答

1

Note: I'm posting this as a completely new answer because the recent edit to your question has made it clear that the real problem is quite different from what it first seemed.

  1. 選擇從data您在導致SortedList<…>想要的子集:

    var subset = data.Where(x => x.Key >= startDate && x.Key <= endDate); 
    
  2. 建立從經過濾項新SortedList

    var sortedListSubset = new SortedList<DateTime, BarData>(); 
    
    foreach (var subsetItem in subset) 
    { 
        sortedListSubset.Add(subsetItem.Key, subsetItem.Value); 
    } 
    

    我不沒有看到更簡單的解決方案問題是Enumerable.Where將返回IEnumerable<KeyValuePair<DateTime, BarData>>,並且沒有擴展方法可將其轉換回SortedList<…>。因此需要逐項遍歷IEnumerable<…>並將它們添加到新的SortedList<…>

When to use Where and when to use Select :

  • Use Where whenever you want to find a subset, ie. when you want to "filter" a collection. (If you know SQL well, Where corresponds to WHERE , hence the name.)

  • Use Select when you want to change/transform, or "project", each element of a collection into something else. In functional programming, this operation is often called "map". (Again, if you know SQL well, then Select — unsurprisingly — corresponds to SELECT .)

Finally, remember that instead of data.Where(x => x.Key >= …) , you could have written:

var subset = from x in data 
       where x.Key >= … 
       select x 

This is some syntactic sugar that C# allows, but essentially means the same thing (however in this form, the select cannot be omitted, even if it doesn't really do any projection). Note again the similarity to SQL!

+0

謝謝斯塔克斯。我希望得到一個不太詳細的解決方案,但已經實施了你的建議。另外,我很欣賞關於Where vs. Select的詳細解釋和建議。 – Ben

+0

@Ben,不客氣。順便說一句:如果你發現給定的答案有幫助,你可以點擊旁邊的向上箭頭符號。如果答案已解決您的問題,您可以檢查旁邊的刻度線。 (這會告訴其他人你的問題已經「回答」了。)[另請參閱常見問題解答「如何在此處提問?」](http://stackoverflow.com/faq#howtoask) – stakx

0

的LINQ選擇方法是接收一個IEnumerable。但是SortedList不會實現這個接口。

+0

如果您使用System.Collections.Generic.SortedList 您將擁有所有LINQ方法。 –

1

它看起來像你使用SortedList作爲一個靜態類,或者你的對象被命名爲類相同。無論如何,這應該工作:

var myList = new SortedList<int, int>(); 
var mySelect = myList.Select(x => x.Value); 
3

Note: You've recently edited your question, stating the real issue, which probably renders this answer obsolete. I've posted a new one to address the "real" issue.


  1. 確保您的項目有System.Core.dll參考。這是.NET程序集,它包含LINQ的幾乎所有集合類型的擴展方法。

    (包括Select,你無法找到SortedListSelect實際上是在靜態類System.Linq.Enumerable發現了一個靜態方法。)

  2. 確保你至少有以下兩個using指令(你已經做):

    using System.Collections; // makes referring to SortedList easier 
    using System.Linq;   // required for the Select method 
    
  3. 由於SortedList是一個非泛型和非類型化的集合,即。它僅包含object s,爲了使用LINQ的Select方法和/或訪問元素的Value屬性,您首先需要調用LINQ的Cast<T>運算符。試試這個:

    var result = sortedList.Cast<TItem>().Select(x => new { 
        x.Value.MyProperty, 
        x.Value.AnotherProperty 
    }); 
    

    其中TItem是項目的類型在你SortedList名稱。

P.S:我假設SortedList在你的例子是指一個局部變量,或字段屬性,而不是作爲類型名稱。這就是爲什麼我將大寫改爲sortedList

P.P.S:除非你有一個很好的理由,否則生活會,如果你使用的System.Collections.Generic.SortedList<TKey, TValue>類,這裏的一些人已經在我之前已經建議變得更容易一些。

相關問題