聚合函数问题

发布于 2024-10-06 18:27:10 字数 461 浏览 1 评论 0原文

    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();
    }
}

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人接听 2024-10-13 18:27:10

看起来您有一个名为 SampleListMyClass 列表,对于其中的每个项目,您想要调用 f_ToString,并创建一个字符串商场。您确实不需要 Aggregate,请尝试(在 .Net 4.0 上):

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

在 .Net 3.5 上,Concat 需要一个数组,所以这将是:

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

如果您仍然想使用 聚合,尽管这里没有充分的理由,但它应该写成:

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

请注意,这里的 counter 是一个字符串,因此您不能对其调用 f_ToString

最后一点,我强烈建议您为变量选择更好的名称。

It looks like you have a list of MyClass called SampleList, and for every item on it you want to call f_ToString, and create one string of them all. You don't really need Aggregate, try (on .Net 4.0):

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

On .Net 3.5 that Concat needs an array, so that would be:

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

If you still do want to use Aggregate, though there's no good reason here, it should be written as:

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

Note that counter is a string here, so you can't call f_ToString on it.

As a final note, I'd warmly advise you to choose better names for your variables.

永不分离 2024-10-13 18:27:10

您尝试为方法 f_ToString() 分配一个值。将 += 替换为 +

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

You try to assign a value to the method f_ToString(). Replace += with +

int AggCount = SampleList.Aggregate((counter, next) => counter.f_ToString() + next.f_ToString());
踏月而来 2024-10-13 18:27:10

因为

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

更改为

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

您使用赋值运算符而不是连接字符串

from

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

change to

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

becus u use assign operator instead of concat string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文