in

Platinum Bay

Peace, Love, and Visual Studio ALM

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.

 

May 20, 2010 9:58 PM

Pingback from  1990 Used Parts Bmw 535i, 535is Lyrics Gasket

 

May 21, 2010 11:27 AM

Pingback from  Carolina Infusion Care, Expedition Cars Explorer Sport Trac

 

May 21, 2010 10:00 PM

Pingback from  E450 Replacement 5.4 L, Be450g Surge Protector

 

May 23, 2010 11:38 AM

Pingback from  Sierra Denali Sale Dallas Fm Stereo, Oldsmobile Sierra - 467.renters.ws

 

May 26, 2010 2:03 AM

Pingback from  260z Replacement Racing Seat Belt 300zx Nissan 280z, Old 1982 280zx Nissan Datsun - 166.rkwrh.com

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
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.