2013-05-06 34 views
0

我有一個具有兩個相關屬性的對象的可查詢集合,RetailerPrice。我想爲每個零售商選擇一個對象(價格最低的那個),然後顯示按零售商名字的第一個字母分組的對象列表,例如,所有對象對應零售商的最低價格,以第一組中的字母'A'開始,下一組爲'B'等。如何將「分組的Linq查詢」還原爲平面IEnumerable?

我很難搞清楚,一旦我分組了我的Linq查詢通過零售商,如何在對象上進行Select查詢而不會將查詢減少到屬性列表並因此丟失對象的引用,例如

var query = from o in myCollection.Cast<MyObjectType>() 
      group o.GetPrice(someParameter) by o.Retailer into oGroup 
      select oGroup.Min() // <-- at this point I've 'lost' the objects 
           //  and just have a list of prices 
           //  I also need to then group the results 
           //  by o.Retailer.ToString()[0] 

我使用的是Windows Phone 7,因此Linq可能會受到限制。

回答

1

所以你只需要零售商和最低價格的關鍵價值對列表? 難道你不能只選擇一個匿名類型,然後像這樣?

var query = from o in myCollection.Cast<MyObjectType>() 
      group o.GetPrice(someParameter) by o.Retailer into oGroup 
      select new { Retailer = oGroup.Key, Min = oGroup.Min() }; 

或者您也可以創建自定義類或KeyValuePair。

+0

我需要的對象,不僅僅是價格和零售商,因爲我需要稍後訪問的對象上還有其他屬性... – Brendan 2013-05-06 22:07:41