If you’re a C# developer moving to javascript, one of the very first things you’ll miss is the ability to write Linq on lists. While Javascript has a couple of methods to deal with arrays, some of them don’t quite match up to the power of Linq. Luckily, there is a great javascript library called Lodash which has almost everything you need! If you are interested in adding Lodash to your Angular project, you can read our guide here : http://angulartut.onpressidium.com/2020/08/02/using-lodash-with-angular/
I’m going to do this page cheatsheet style and simply list out the Linq method with it’s Lodash equivalent. Feel free to bookmark this page and come back to it when you start scratching your head thinking “I know how to do this in C#…” as quite often, it’s just a simple rename of your method and you are good to go.
My Models
For the sake of simplicity, the model I will be using will look like so :
{
people : [
{
"firstName" : "John"
"lastName" : "Smith"
"age" : 30
}
]
}
Lodash / C# Linq Cheatsheet
All
//C#
items.All(x => x.Age == 30);
//Lodash
_.every(items, x => x.age === 30);
OR
_.every(items, {'age': 30});
Any
//C#
items.Any(x => x.Age == 30);
//Lodash
_.some(items, x => x.age === 30);
OR
_.some(items, {'age': 30});
Average
//C#
items.Average(x => x.Age == 30);
//Lodash
_.meanBy(items, x => x.age === 30);
OR
_.meanBy(items, 'age');
Contains
//C#
items.Contains(myItem);
//Lodash
_.includes(items, myItem);
Count
Count how many match a predicate, not just the length of the array. Note that the return object for Lodash is essentially a group by on the predicate. For example with a collection with two items, one with an age of 30 and the other of an age of 31. The result is :
{
false: 1,
true: 1
}
//C#
items.Count(x => x.Age == 30);
//Lodash
_.countBy(items, x => x.age === 30);
OR
_.countBy(items, {'age' : 30});
Distinct
Note that in different versions of Lodash, the functions unique, uniq, and uniqBy have all been swapped around so you may need to try a variant of those depending on your lodash version.
//C#
items.Distinct(x => x.FirstName);
//Lodash
_.unique(items, x => x.firstName);
OR
_.unique(items, 'firstName')
First / FirstOrDefault
Lodash doesn’t provide functionality to either throw an exception like the C# First, or to return null like FirstOrDefault. Instead it will always return an empty array ([])
//C#
items.First(x => x.Age == 30);
//Lodash
_.first(items, x => x.age === 30);
OR
_.first(items, {'age': 30})
Foreach
//C#
items.ForEach(x => x.Age = 30);
//Lodash
_.forEach(items, x => x.age = 30);
Last / LastOrDefault
Similar to First / FirstOrDefault, Lodash doesn’t provide functionality to either throw an exception like the C# Last, or to return null like LastOrDefault. Instead it will always return an empty array ([])
//C#
items.Last(x => x.Age == 30);
//Lodash
_.last(items, x => x.age === 30);
OR
_.last(items, {'age': 30})
Max
Max in C# linq returns only the value of the property (e.g. If the max age is 30, it will return the value 30, not the actual array item). Lodash however will return the entire item that has the highest property value. If you want the entire item in C#, you have to use OrderBy/OrderByDescending.
//C#
items.Max(x => x.Age);
//Lodash
_.max(items, x => x.age);
OR
_.max(items, 'age'})
Min
Similar to Max, C# will return only the property while Lodash will return the entire item.
//C#
items.Min(x => x.Age);
//Lodash
_.min(items, x => x.age);
OR
_.min(items, 'age'})
OrderBy / OrderByDescending
Some versions of Linq only support sortBy which only sorts in ascending value. Similar to C#, orderby is stable, that is, the original order of the items is kept in tact and you must read the resulting object.
//C#
var sortedItems = items.OrderBy(x => x.Age);
OR
var sortedItems = items.OrderByDescending(x => x.Age);
//Lodash
var sortedItems = _.orderBy(items, ['age']);
OR
var sortedItems = _.orderBy(items,['age'], ['desc']);
Select
//C#
items.Select(x => x.FirstName);
//Lodash
_.map(items, x => x.firstName)
Skip
//C#
items.Skip(10);
//Lodash
_.slice(items, 10)
Sum
//C#
items.Sum(x => x.Age);
//Lodash
_.sumBy(items, x => x.age);
OR
_.sumBy(items, 'age');
Take
//C#
items.Take(10);
//Lodash
_.slice(items, 0, 10)
Where
//C#
items.Where(x => x.Age == 30)
//Lodash
_.filter(items, x => x.age === 30);
💬 Leave a comment