Created
May 13, 2011 07:28
-
-
Save joelnet/970138 to your computer and use it in GitHub Desktop.
Quick examples on how to use MiniLinq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// works in a similar way to LINQ with some minor differences. | |
using System; | |
using System.Collections; | |
using System.Runtime.CompilerServices; | |
namespace MiniLinqApi | |
{ | |
[IgnoreNamespace] | |
internal static class MiniLinq | |
{ | |
public static LinqObject From(object list) | |
{ | |
LinqObject value = new LinqObject(list); | |
return value; | |
} | |
} | |
[IgnoreNamespace] | |
internal class LinqObject | |
{ | |
Array list; | |
ArrayItemMapCallback select; | |
ArrayItemFilterCallback where; | |
CompareCallback orderby; | |
public LinqObject(object list) | |
{ | |
this.list = Array.ToArray(list); | |
} | |
public LinqObject Where(ArrayItemFilterCallback where) | |
{ | |
this.where = where; | |
return this; | |
} | |
public LinqObject OrderBy(CompareCallback orderby) | |
{ | |
this.orderby = orderby; | |
return this; | |
} | |
[AlternateSignature] | |
public extern LinqObject Select(); | |
[AlternateSignature] | |
public LinqObject Select(ArrayItemMapCallback select) | |
{ | |
if (select != null) | |
{ | |
this.select = select; | |
} | |
return this; | |
} | |
public int Count() | |
{ | |
return ToArray().Length; | |
} | |
public object FirstOrDefault() | |
{ | |
ArrayList tempList = new ArrayList(); | |
// filter by 'where' | |
foreach (object item in list) | |
{ | |
if (where == null || where(item)) | |
{ | |
tempList.Add(item); | |
if (orderby == null) | |
{ | |
break; | |
} | |
} | |
} | |
// perform 'orderby' | |
if (orderby != null) | |
{ | |
tempList.Sort(orderby); | |
} | |
// process 'select' | |
if (select != null) | |
{ | |
for (int i = 0; i < tempList.Count; i++) | |
{ | |
tempList[i] = select(tempList[i]); | |
} | |
} | |
return tempList.Count > 0 ? tempList[0] : null; | |
} | |
public Array ToArray() | |
{ | |
return (Array)ToList(); | |
} | |
public ArrayList ToList() | |
{ | |
ArrayList value = new ArrayList(); | |
// filter by 'where' | |
foreach (object item in list) | |
{ | |
if (where == null || where(item)) | |
{ | |
value.Add(item); | |
} | |
} | |
// perform 'orderby' | |
if (orderby != null) | |
{ | |
value.Sort(orderby); | |
} | |
// process 'select' | |
if (select != null) | |
{ | |
for (int i = 0; i < value.Count; i++) | |
{ | |
value[i] = select(value[i]); | |
} | |
} | |
return value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this works with objects too, int used for simplicity. | |
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
// [2, 4, 6, 8, 10] | |
int[] evenNumbers = (int[])MiniLinq | |
.From(numbers) | |
.Where(delegate(object o) { return (int)o % 2 == 0; }) | |
.ToArray(); | |
// [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | |
int[] descendingNumbers = (int[])MiniLinq | |
.From(numbers) | |
.OrderBy(delegate(object x, object y) { return x == y ? 0 : (int)x > (int)y ? -1 : 1; }) | |
.ToArray(); | |
// [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] | |
int[] doubledNumbers = (int[])MiniLinq | |
.From(numbers) | |
.Select(delegate(object o) { return (int)o * 2; }) | |
.ToArray(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment