Here are some usage examples of the extensions provided in the MicroLinq package.

The examples below are based on the following collection.

 

ArrayList notes = new ArrayList();
notes.Add("Do");
notes.Add("Re");
notes.Add("Mi");
notes.Add("Fa");
notes.Add("So");
notes.Add("La");
notes.Add("Ti");
notes.Add("Do");

 

Returns an objet that represents the aggregate of the items in an IEnumerable defined
by the delegate supplied. An optional seed parameter is used as the aggregate collector.
notes.Aggregate(string.Empty, (agg, item) => string.Concat(agg, item)); // "DoReMiFaSoLaTiDo"
notes.Aggregate(string.Empty, (agg, item) => string.Concat(agg, item), res => ((string)res).Substring(2, 7)); // "ReMiFaS"

 

Returns true if all objects in the IEnumerable satisfy the predicate condition.
notes.All(note => ((string)note).Equals("Do")); // False

 

Returns true if any object in the IEnumerable satisfies the predicate condition.
notes.Any(note => ((string)note).Equals("Mi")); // True

 

Returns a count of the number of elements in the IEnumerable.
notes.Count(); // 8

 

Returns a count of the number of elements in a IEnumerable which satisfy a condition.
notes.Count(note => ((string)note).Equals("Do")); // 2

 

Returns the first object in an IEnumerable which satisfies a condition or null.
notes.FirstOrDefault(note => ((string)note).Equals("Frank")); // null

 

Returns a wrapper around an IEnumerable to return the combined results of the elements and the supplied delegate.
notes.Select(note => string.Concat("-", note, "!")); // { "-Do!", "-Re!", "-Mi!", "-Fa!", "-So!", "-La!", "-Ti!", "-Do!" }

 

Returns a wrapper around an IEnumerable to return elements matching the requested type.
notes.OfType(typeof(string)); // { "Do", "Re", "Mi", "Fa", "So", "La", "Ti", "Do" }

 

Copies the elements from an IEnumerable into an Array of objects. A simple bubble sort
is applied to the new list by applying the Selector delegate to each object element and
casting it to IComparable (or a reflected invoke of CompareTo) to perform the comparison.
If the IEnumerable can be cast to IList, the values will be sorted in place. This will
require half as much memory but will modify the existing order of data.
notes.OrderBy(note => note); // { "Do", "Do", "Fa", "La", "Mi", "Re", "So", "Ti" }

 

Returns a wrapper around an IEnumerable to return elements matching the supplied condition.
notes.Where(note => ((string)note).Substring(0,1).Equals("M")); // "Mi"

Last edited Feb 18, 2011 at 4:24 AM by VikingErik, version 6

Comments

No comments yet.