聚合函数问题
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您有一个名为
SampleList
的MyClass
列表,对于其中的每个项目,您想要调用f_ToString
,并创建一个字符串商场。您确实不需要Aggregate
,请尝试(在 .Net 4.0 上):在 .Net 3.5 上,Concat 需要一个数组,所以这将是:
如果您仍然想使用 聚合,尽管这里没有充分的理由,但它应该写成:
请注意,这里的
counter
是一个字符串,因此您不能对其调用f_ToString
。最后一点,我强烈建议您为变量选择更好的名称。
It looks like you have a list of
MyClass
calledSampleList
, and for every item on it you want to callf_ToString
, and create one string of them all. You don't really needAggregate
, try (on .Net 4.0):On .Net 3.5 that Concat needs an array, so that would be:
If you still do want to use Aggregate, though there's no good reason here, it should be written as:
Note that
counter
is a string here, so you can't callf_ToString
on it.As a final note, I'd warmly advise you to choose better names for your variables.
您尝试为方法 f_ToString() 分配一个值。将 += 替换为 +
You try to assign a value to the method f_ToString(). Replace += with +
因为
更改为
您使用赋值运算符而不是连接字符串
from
change to
becus u use assign operator instead of concat string