uzay95
uzay95

Reputation: 16642

Aggregate function problem

    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

Upvotes: 1

Views: 179

Answers (3)

Kobi
Kobi

Reputation: 138137

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.

Upvotes: 2

Bonshington
Bonshington

Reputation: 4032

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

Upvotes: 1

Torbj&#246;rn Hansson
Torbj&#246;rn Hansson

Reputation: 19423

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

Upvotes: 1

Related Questions