2010-12-07 109 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字符串