Learn 6 ES6 Javascript Equivalents to C# LINQ Methods
Tags: c, javascript, es6, net
Published: 2020-03-23 15:13:50

---
title: Learn 6 ES6 Javascript Equivalents to C# LINQ Methods
published: true
description: Find the JavaScript ES6 equivalents Where, Select, OrderBy, Aggregate, All & many other C# LINQ filtering operators.
tags: C, Javascript, ES6, net
canonical_url: https://andyp.dev/posts/learn-6-es6-javascript-equivalents-c-linq-methods
cover_image: https://andyp.dev/images/article/2020/learn-6-es6-javascript-equivalents-c-linq-methods/learn-6-es6-javascript-equivalents-c-linq-methods.png
---


I like many others, have a love/hate relationship with Javascript.

Most of my time is spent developing enterprise IT systems in C# but i've recently promised myself that I'd spend some time to really learn modern Javascript.

I've been using modern Javascript for several years but always found myself forgetting what all the ES6 methods mean. In my opinion, "Map" isn't really memorable, even to a native English speaker when compared to the C# equivalent "Select".

So I made a list of some commonly used ES6 methods in Javascript, with a short explanation and details of their C#/LINQ counterparts.

## Our Dataset

If you need to test any of the methods i'm going to talk about, feel free to to test them in Javascript using the below dataset.

    var people = [ 
    	{ name: "James", age: 20 }, 
    	{ name: "Bob", age: 31 }, 
    	{ name: "Harry", age: 18 }, 
    	{ name: "Linus", age: 23 }, 
    	{ name: "Barry", age: 50 }, 
    	{ name: "Hillary", age: 45 },
    	{ name: "Peter", age: 22 } 
    ];

## Where & Filter

Javascript's Filter method works the same way you would expect the Where method to work in LINQ.

    var items = people.filter(function (item) {
    	return item.age > 25;
    });

    console.log("People older than 25");
    console.log(items);

Filter creates a new array of items which pass the specified logic. In our example, we create a list of people older than 25.

## Select & Map

The Map method in Javascript works the same way you would expect Select to work in LINQ.

    var names = people.map(function (item) {
    	return item.name;
    });

    console.log("Just the names of people in the array");
    console.log(names);

Essentially by using the Map method we create a new array of entries from the old array, however we only pull the values that we have specified.

Think of this like creating a new dataset based on specific values from a database, but we're specifying the column names and only pulling values from the columns that we have specified.

## All & Every

The Every method in Javascript is equivalent to All in LINQ.

    var allUnder20 = people.every(function (item) {
    	return  item.age < 20;
    });

    console.log("Are all people older than 20");
    console.log(allUnder20);

Every lets us test that all items within an array match the test criteria. You will receive either a true or false response from the method. 

## Some & Any

In javascript we have the Some method which is equivalent to Any in LINQ.

    var anyOver20 = people.some(function (item) {
    	return  item.age > 20;
    });

    console.log("Are any people over 20?"); 
    console.log(anyOver20 );

By using Some, we can check if any items within an array match the criteria that we have specified. If a single item matches our criteria then a true response is sent, otherwise it will be false.

## OrderBy & Sort

The Sort method in Javascript is a close match to the OrderBy LINQ method.

    var sortedByName = people.sort(function (a, b) {
    	return  a.name > b.name ? 1 : 0;
    })

    console.log("The list ordered by name"); 
    console.log(sortedByName);

Sort allows us to sort the array of elements by a specific value shared between all items within the array.

## Aggregate & Reduce

The final method on our list is Reduce, which is the equvalent to the Aggregate method we use in LINQ. 

    var aggregate = people.reduce(function (item1, item2) {
    	return  { name: '', age: item1.age + item2.age };
    });

    console.log("Aggregate age"); 
    console.log(aggregate.age);

Essentially the Reduce method lets us reduce all of the specified values in the array into a single value. In our example we are adding all of the ages from all people, and then printing the output to the console.

## Final Notes

Many of the naming conventions used for the JS methods seem strange to many, including myself. That said, I'd recommend anyone who ever touches the frontend to learn (or at least be aware of) these modern JS methods.

I hope this has been useful.