2010-12-07 134 views
1
List<MyClass> SampleList = new List<MyClass>() { new MyClass(), new MyClass(), new MyClass() }; 
    string AggCount = SampleList.Aggregate((counter, next) => counter.f_ToString() += next.f_ToString()); 
} 

} 

internal class MyClass 
{ 
    public string f_ToString() 
    { 
     Random rnd = new Random(); 
     return rnd.Next(1000).ToString(); 
    } 
} 

alt text聚合函數問題

+3

你究竟在做什麼? – LukeH 2010-12-07 13:05:41

+0

你的問題實際上是什麼?這個消息實際上是非常明確的:你不能使用調用方法作爲你想要分配的東西 – PierrOz 2010-12-07 13:06:17

回答

2

它看起來像你H請撥打MyClass的清單,名稱爲SampleList,對於每個項目,您都需要撥打f_ToString,然後爲其創建一個字符串。你並不真的需要Aggregate,試圖(對.NET 4.0):

string agg = String.Concat(SampleList.Select(myClass => myClass.f_ToString())); 

基於.NET 3.5是需要的毗連一個數組,所以這將是:

string agg = String.Concat(
    SampleList.Select(myClass => myClass.f_ToString()).ToArray() 
    ); 

如果你還是想使用Aggregate,雖然這裏沒有很好的理由,應該寫成:

string agg = SampleList.Aggregate("", 
        (counter, next) => counter + next.f_ToString()); 

注意counter這裏是一個字符串,所以你不能叫f_ToString在上面。

作爲最後一點,我會熱烈地建議您爲變量選擇更好的名稱。

1

您嘗試將值賦給方法f_ToString()。更換+ =+

int AggCount = SampleList.Aggregate((counter, next) => counter.f_ToString() + next.f_ToString()); 
1

int AggCount = SampleList.Aggregate((counter, next) => counter.f_ToString() += next.f_ToString()); 

變化

int AggCount = SampleList.Aggregate((counter, next) => counter.f_ToString() + next.f_ToString()); 

becus u使用賦值運算符,而不是CONCAT字符串