Conversions
Conversion operations change the type of objects and find use in a variety of tasks including, but not limited to the following uses:
- Achieve hiding of a type's custom implementation of a standard query operator with Enumerable.AsEnumerable<TSource>.
- Enable non-parameterized collections for use in querying with Enumerable.OfType<TResult>.
- Force immediate execution of a query rather than delaying it for enumeration by using Enumerable.ToArray<TSource>, Enumerable.ToDictionary<TSource, Tkey>, Enumerable.ToList<TSource>, and Enumerable.ToLookup<TSource, Tkey>.
The following query operator methods perform conversion operations:
- AsEnumerable
- AsQueryable
- Cast
- OfType
- ToArray
- ToDictionary
- ToList
- ToLookup
The AsEnumberable method returns the input typed in the form of IEnumberable<T>. Review its syntax below:
public static IEnumerable<TSource> AsEnumerable<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
class Clump<T> : List<T> { public IEnumerable<T> Where(Func<T, bool> predicate) { Console.WriteLine("Clump's Where() implementation."); return Enumerable.Where(this, predicate); } } static void AsEnumerableOne() { Clump<string> genreClump = new Clump<string> { "jazz", "funk", "soul", "rock", "country", "blues", "gospel", "pop" }; IEnumerable<string> queryOne = genreClump.Where(genre => genre.Contains("o")); Console.WriteLine("queryOne spawned.\n"); IEnumerable<string> queryTwo = genreClump.AsEnumerable().Where(genre => genre.Contains("o")); Console.WriteLine("queryTwo spawned."); }
The AsQueryable method converts an IEnumberable into IQueryable. It offers a wealth of different uses such as mock testing, reuse polymorphism, and changing compile time types. Review its syntax below:
public static IQueryable AsQueryable( this IEnumerable source )
Review an example of its use:
public class Player { public int ID { get; set; } public string GivenName { get; set; } public string SurName { get; set; } public List<int> PPG { get; set; } public Player(int Id, string givenName, string surName, List<int> ppg) { this.ID = Id; this.GivenName = givenName; this.SurName = surName; this.PPG = ppg; } } public class Players<T> : List<T> { } public void AsQueryableEX() { Players<Player> players = new Players<Player>() { new Player(201, "Joseph", "Amenowode", new List<int>() { 12, 7, 10, 15 }), new Player(202, "George", "Ayittey", new List<int>() { 15, 8, 9, 11 }), new Player(203, "Kwame", "Appiah", new List<int>() { 9, 4, 12, 7 }), new Player(204, "Lawrence", "Ofosu-Appiah", new List<int>() { 2, 5, 16, 4 }), new Player(205, "Kimathi", "Donkor", new List<int>() { 6, 7, 0, 2 }) }; IQueryable<Player> queryOne = players.AsQueryable().Where(player => player.SurName == "Appiah"); Response.Output.Write("{0}<br />", queryOne.First().GivenName); Response.Output.Write("1st query executed.<br /><br />"); }
The Cast method casts IEnumberable elements to a specified type. Review its syntax below:
public static IEnumerable<TResult> Cast<TResult>( this IEnumerable source )
Review an example below:
System.Collections.ArrayList girls = new System.Collections.ArrayList(); girls.Add("Toni"); girls.Add("Stacey"); girls.Add("Apollonia"); IEnumerable<string> query = girls.Cast<string>().OrderBy(girl => girl).Select(girl => girl); foreach (string girl in query) { Console.WriteLine(girl); }
The ToArray method converts a collection into an array, and also forces query execution. Review its syntax below:
public static TSource[] ToArray<TSource>( this IEnumerable<TSource> source )
Review an example below:
class Product { public string Distro { get; set; } public int Units { get; set; } } public static void ToArrayOne() { List<Package> products = new List<Product> { new Product { Distro = "Pune Warehouse", Units = 20 }, new Product { Distro = "Chicago Warehouse", Units = 30 }, new Product { Distro = "Houston Warehouse", Units = 61 }, new Product { Distro = "Shanghai Warehouse", Units = 5 } }; string[] distros = products.Select(pdt => pdt.Distro).ToArray(); foreach (string distro in distros) { Console.WriteLine(distro); } }
The ToDictionary method places elements into a Dictionary<TKey, Tvalue> based on a key selector function, and it also forces query execution. Review its syntax below:
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector )
Review an example below:
class Product
{
public string Distro { get; set; }
public int Units { get; set; }
public long TrackNo { get; set; }
}
public static void ToDictionaryOne()
{
List<Product> products =
new List<Product>{
new Product { Distro = "Oakland Warehouse", Units = 26, TrackNo = 464864846 },
new Product { Distro = "London Warehouse", Units = 19, TrackNo = 89114612 },
new Product { Distro = "Jakarta Warehouse", Units = 5, TrackNo = 299651684 },
new Product { Distro = "Guangzhou Warehouse", Units = 34, TrackNo = 461161846 }
};
// Use TrackNo as the key.
Dictionary<long, Product> dictionary =
products.ToDictionary(p => p.TrackNo);
foreach (KeyValuePair<long, Product> kvp in dictionary)
{
Console.WriteLine(
"Key {0}: {1}, {2} units",
kvp.Key,
kvp.Value.Distro,
kvp.Value.Units);
}
}
The ToList method converts a collection into a List<T>, and it forces query execution. Review its syntax below:
public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source )
Review an example below:
string[] snacks = { "chips", "cashews", "banana", "cookies", "icecream", "candybar", "buns"}; List<int> lengths = snacks.Select(snack => snack.Length).ToList(); foreach (int length in lengths) { Console.WriteLine(length); }