MiniLinq for Script#

Tags: script#, c#, javascript, linq

Script# is my new favorite piece of technology. Write JavaScript with C#? Yes please! But since it's (currently) limited to ISO-2 C#, it can feel like you're stuck back in 2004. Using C# 2 means you're missing a lot of things you use daily... like LINQ. I do miss LINQ. So I read about Script# adding a few LINQ like extensions to the array class and decided to work them together into this MiniLinq class, which I am now sharing with you.

Some examples

This works with objects too, I'm just using int 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();

I'm calling it MiniLinq because it's only a partial implementation of LINQ. To be more accurate, it's just the methods I need to use for my project. Until there's an official implementation in Script#, this is what I'll be using. If you want to download or extend MiniLinq, check it out on github. Share your changes with me!

Add a Comment