JavaScript functions vs LINQ C#
Tags: csharp, javascript, functional, linq
Published: 2020-06-26 15:58:58

---
title: JavaScript functions vs LINQ C#
published: true
description: This is a comparison between Javascript functions and LINQ in C# for collections 
tags: csharp,javascript,functionalprogramming, linq
//cover_image: https://direct_url_to_image.jpg
---

I have been working with C# for 10 years and last 4 years I started to use JS in the front-end into deep. 

I want to compare some methods related to arrays and collection in JS and C# (using LINQ). I want to show you how to choose the right method depending on your requirements in JS or C#.  

**forEach (js) vs ForEach (C# not LINQ):**
This method is used to execute a function for each element in the array or collection. You can use it to update each element depending on conditions or for getting specific values.
Both methods are not pure function which means the original collection is affected or updated. This method is not included in LINQ (it's on C# collections directly) but it's important to mention it. 

```javascript
//JS demo
const array1 = [1, 2, 3, 4, 5];

array1.forEach(element => console.log(element));
```
```csharp
//C# demo
var listOfNumbers =  new int[] {0,1,2,3,4,5};

listOfNumbers.ToList().ForEach(p => Console.WriteLine(p));
```

**filter (js) vs Where (LINQ):**
This method is used to make a filter by a function depending on a condition.
Both methods are pure functions and return new collections including the record that matches the condition.

```javascript
//JS demo
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

```
```csharp
//C# demo
var listOfNumbers =  new int[] {0,1,2,3,4,5};

var evenNumbers = listOfNumbers.ToList().Where(p => p%2 == 0);
```

**reduce (js) vs Aggregate (LINQ):**
This method executes a function for each element to return only one value.
Both methods are pure functions and return a new single value with no affectations in the original collection.

```javascript
//JS demo
const array1 = [1, 2, 3, 4, 5];

const reducer = array1.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};

var sumTotal = listOfNumbers.Aggregate(0,(total, currentItem)=> total+ currentItem);
```

**sort (js) vs OrderBy (LINQ):**
This method sorts the elements in the collection depending on a function or a parameter. 
in JS this method is not a pure function and this method updates the original collection. However, in C# this method is a pure function returning a new collection and can sort easily depending on the property selected. Also, you can use the method 'Then' to sort the new collection by other properties. 

```javascript
//JS demo
const months = ['March', 'Jan', 'Feb', 'Dec'];

months.sort();

```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};

var numberSorted = listOfNumbers.OrderBy(p=> p);
//var listOfUsers = users.OrderBy(p=> p.Name).Then(p=> p.LastName);
```

**concact (js) vs Concact (LINQ):**
This method is used to merge into collections into one array. 
Both methods are pure functions and return a new collection including the records from the original collection.

```javascript
//JS demo
const array1 = [1,2,3,4,5];
const array2 = [6,7,8,9,10];
const array3 = array1.concat(array2);
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};
var listOfNumbers2 =  new int[] {6,7,8,9,10};
		
var allNumbers = listOfNumbers.Concat(listOfNumbers2);
```

**push (js) vs Append (LINQ):**
This method is used to add a new element to add the end of the list. Both methods update the original collection.

```javascript
//JS demo
var array1 = [1,2,3,4,5];
array1.push(6);
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};
listOfNumbers.Append(6);
```

**find (js) vs FirstOrDefault (LINQ):**
This returns the first item which satisfies the criteria specified. These methods don't affect or update the original collection (pure functions). 

```javascript
//JS demo
var array1 = [1,2,3,4,5];
var item1 = array1.find(p=> p===1);
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};
var item1 = listOfNumbers.FirstOrDefault(p=> p==1);
```

**includes (js) vs Any (LINQ):**
This method returns a boolean whether an array contains an element in JS. In LINQ, the method 'Any' returns a boolean whether at least an element satisfies the condition specified. These methods don't affect or update the original 

```javascript
//JS demo
var array1 = [1,2,3,4,5];
var result = array1.includes(1);
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};
var result = listOfNumbers.Any(p=> p==1);
```
**reverse (js) vs Reverse (LINQ):**
This method in JS reverses an array by modifying the original array and also returning a new one reversed. In LINQ this method is a pure function and returns a new array reversed with no affections on the original collection. 

```javascript
//JS demo
var array1 = [1,2,3,4,5];
var newArray = array1.reverse(); //array1 is reversed
```
```csharp
//C# demo
var listOfNumbers =  new int[] {1,2,3,4,5};
var newListOfNumbers = listOfNumbers.Reverse();
```