public
Last active

Quick examples on how to use MiniLinq

  • Download Gist
MiniLinq.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
// 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;
}
 
}
 
}
MiniLinqDemo.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.