Linqjs Tutorial #01 - Nodejs with Lambda Expression (Install, Sum, Avg, Min, Max, Select)
Repository
https://github.com/mihaifm/linq
What Will I Learn?
- You will learn about linqjs.
- You will learn how to install linqjs with your project.
- You will learn how to summation values using linqjs.
- You will learn how to average values using linqjs.
- You will learn how to minimum values using linqjs.
- You will learn how to maximum values using linqjs.
- You will learn how to select specific column values using linqjs.
Requirements
- Node.JS
- Basic knowledge of Javascript
- Any suitable code editor
Difficulty
- Basic/Intermediate
Tutorial Contents
1. About linqjs
LINQ (Language Integrated Query) is query syntax in C# and VB.NET for saving and retrieving data from different sources. Linqjs is a javascript implementation of the .NET LINQ library. Which allows saving and retrieving data from json or array. It contains all the origial .NET methods with some extra methods.
2. Install
npm install linq
At the start of the javascript file add this line.
const rlinq = require('linq');
This line will include linqjs package to the script so we can work with it.
Declare an array which will be used on our following steps of tutorial
We have declared an array of products. which contains products id, name and its price.
var products = [
{ "id": 100, "name": "Pendrive", "price": 500 },
{ "id": 101, "name": "Ram", "price": 1000 },
{ "id": 102, "name": "Motherboard", "price": 5000 },
{ "id": 103, "name": "Keyboard", "price": 450 }
]
3. Summation
Our target is to summation the total price of the products. Our following single line of code using linqjs will summarize the total price of products.
var totalprice = rlinq.from(products).sum("$.price");
Explanation :
- rlinq is the references of linqjs
- we have passed products list on the parameter of from function
- we mentioned price to sum by passing $.price(lambda expression) as a parameter of sum function
- this will return the summarize price of products
4. Average
Our target is to calculate the average price of the products. Our following single line of code using linqjs will return the average price of the products.
var averageprice = rlinq.from(products).average("$.price");
Explanation :
- rlinq is the references of linqjs
- we have passed products list on the parameter of from function
- we mentioned price to calculate average by passing $.price(lambda expression) as a parameter of average function
- this will return the average price of products
5. Minimum
Our target is to find out minimum price of the products. Our following single line of code using linqjs will return the minimum price.
var minimumprice = rlinq.from(products).min("$.price");
Explanation :
- rlinq is the references of linqjs
- we have passed products list on the parameter of from function
- we mentioned price to find minimum by passing $.price(lambda expression) as a parameter of min function
- this will return the minimum price of products
6. Maximum
Our target is to find out maximum price of the products. Our following single line of code using linqjs will return the maximum price.
var maximumprice = rlinq.from(products).max("$.price");
Explanation :
- rlinq is the references of linqjs
- we have passed products list on the parameter of from function
- we mentioned price to find maximum by passing $.price(lambda expression) as a parameter of max function
- this will return the maximum price of products
7. Select
Our target is to select all products name in a array. Our following single line of code using linqjs will return all products name.
var productName = rlinq.from(products).select("$.name").toArray();
Explanation :
- rlinq is the references of linqjs
- we have passed products list on the parameter of from function
- we mentioned name to find maximum by passing $.name(lambda expression) as a parameter of select function
- toArray function will make a list of product name from original product list
Proof of Work Done
Tutorial Code
GitHub: https://github.com/touhidalam69/linqjsTutorial
Great tutorial thanks for sharing.
Thanks for your complement
Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:
Looking forward to your upcoming tutorials.
See in this link an example of a good tutorial.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Hello, @portugalcoin59,
Thanks for your nice observation about my tutorial post. Actually this is my first contribution post on this category. I will keep it mind all of your advices on my following contribution.
Thanks again ☺☺☺
In addition to @portugalcoin's notes, your tutorial does not really capture a proper structure nor content of a tutorial.
Please work on providing better quality tutorials in the future.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Hi @mcfarhat66,
Thanks for asking me about my mistakes on this tutorial post.
I will be aware in future contribution.
Thanks again 😌😌😌