in

Platinum Bay

Peace, Love, Team System, and Community

This Blog

Syndication


.NETicated

LINQ: Porting jQuery’s Each

As some astute readers pointed out, this functionality does in fact exist in LINQ. The name isn’t terribly obvious however: Aggregate. I was able to update the original tests to use the Aggregate method. The main difference is that a non-generic IEnumerable does not contain this functionality. For that type I had to use:

.OfType<>()

The updated test methods are here:

IEnumerable<string> collection = new[] { "Hello", "World", "!" };
string result = collection.Aggregate((str1, str2) => (str1 + " " + str2).Trim());
IEnumerable<int> collection = new[] { 1, 2, 3, 4, 5 };
int result = collection.Aggregate((num1, num2) => num1 + num2);
IEnumerable collection = new[] { "Hello", "World", "!" };
string result = collection.OfType<string>().Aggregate((string str1, string str2) => (str1 + " " + str2).Trim());
var collection = new List<string> { "Hello", "World", "!" };
string result = collection.Aggregate((str1, str2) => (str1 + " " + str2).Trim());

I love learning new stuff, thanks to Steve Gilham and Jordan Terrell for their comments below.

 

I really like the Each function in jQuery, and I find myself wanting to use it in LINQ. Since it doesn’t exist I decided to write one. Here goes:

using System;
using
System.Linq;
using
System.Collections;
using
System.Collections.Generic;

public static class
ExtensionMethods
{
public static TType Each<TType>(this IEnumerable<TType> collection, Func
<TType, TType, TType> func)
{
var result = default
(TType);

foreach (var queryable in
collection)
result = func(result, queryable);

return
result;
}

public static TType Each<TType>(this IEnumerable collection, Func
<TType, TType, TType> func)
{
return
collection.OfType<TType>().Each(func);
}
}

You can then call these extension methods using any IEnumerable type, such as List, Collection, etc. I’m utilizing lambdas to define inline methods, though a delegate to an existing method could be passed in as well.

IEnumerable<string> collection = new[] { "Hello", "World", "!" };
string result = collection.Each((str1, str2) => (str1 + " " + str2).Trim());
IEnumerable<int> collection = new[] { 1, 2, 3, 4, 5 };
int result = collection.Each((num1, num2) => num1 + num2);
IEnumerable collection = new[] { "Hello", "World", "!" };
string result = collection.Each((string str1, string str2) => (str1 + " " + str2).Trim());
var collection = new List<string> { "Hello", "World", "!" };
string result = collection.Each((str1, str2) => (str1 + " " + str2).Trim());

What do you think? Would you do it differently?

Published Aug 26 2009, 07:26 PM by Steve
Filed under: ,

Comments

August 27, 2009 5:25 AM

Pingback from  Dew Drop &#8211; August 27, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

August 27, 2009 6:48 AM

It exists, but is called by the less than immediately apparent name "Aggregate" (took me a while to find it, too! -- I was looking for Fold or FoldL)

It comes in three different overloads.  to allow an initial value and type transformation as well the basic version as above.  See

msdn.microsoft.com/.../system.linq.enumerable.aggregate.aspx

 

August 27, 2009 8:28 AM

This looks like the Aggregate(...) method already provided in LINQ - not one of the well known LINQ extension methods.

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is a Team System MVP and INETA Speaker, and has been working as a developer for more than 9 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. Steve is also an MCP, ICSOO, Speaker Liaison for the Philly .NET User Group, and community fanatic.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.