Firstly, set a sequence.List myList = new List { 1, 2, 3, 4 ,5};Now find the sum using the Queryable Sum() method.myList.AsQueryable().Sum();Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { List myList = new List { 1, 2, 3, 4 ,5}; Console.WriteLine("Sum of elements in a list..."); foreach (int res in myList) { Console.WriteLine(res); } int sum = myList.AsQueryable().Sum(); Console.WriteLine("Sum = {0}", sum); } }OutputSum of elements in a list... 1 2 3 4 5 Sum = 15
To add properties and methods to an object in JavaScript, use the prototype property.ExampleYou can try to run the following code to learn how to work with prototype − JavaScript prototype property function book(title, author) { this.title = title; this.author = author; } var myBook = new book("Amit", "Java"); book.prototype.price = null; myBook.price = 500; document.write("Book title is : " + myBook.title + ""); document.write("Book author is : " + myBook.author + ""); document.write("Book price is : " + myBook.price + "");
Get specified number of elements from the beginning using the Take() method.The following is our array.int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 };Now, use OrderByDescending to order the elements in Descending order. Then use the Take() method to get the elements.marks.AsQueryable().OrderByDescending(s => s).Take(5);Let us see the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 }; // top 5 student marks IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Take(5); foreach (int res in selMarks) { Console.WriteLine(res); } } }Output95 90 85 72 67
Get specified number of elements from the end using the TakeLast() method.The following is our array.int[] pages = { 492, 290, 129, 602, 152 };Now, use OrderBy to order the elements in ascending order. Then use the TakeLast() method to get specified number of elements from the end.marks.AsQueryable().OrderByDescending(s => s).Take(5);Let us see the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { // pages of books int[] pages = { 492, 290, 129, 602, 152 }; // get pages of last two ... Read More
Use the reduceRight() method in JavaScript to apply a function simultaneously against two values of the array from right-to-left as to reduce it to a single value.The following are the parameters −callback − Function to execute on each value in the array.initialValue − Object to use as the first argument to the first call of the callbackExampleYou can try to run the following code to learn how to work with reduceRight() method in JavaScript − JavaScript Array reduceRight Method if (!Array.prototype.reduceRight) ... Read More
Get elements as long as the condition is true in a sequence using the TakeWhile() method.The following is our list with strings.IList str = new List(){ "Car", "Bus", "Truck", "Airplane"};Now, let’s say we need the strings whose length is less than 4. For that, use Lambda Expressions and add it as a condition in the TakeWhile() method.str.TakeWhile(a => a.Length < 4);Here is the example that displays elements until the condition is trie.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList str = new List(){ "Car", "Bus", "Truck", "Airplane"}; ... Read More
Use ThenBy() method to order array elements. Let’s say we have the following string array.string[] str = { "Sandler", "Jack", "Tom", "Matt", "Henry", "Johnny" };Now, use Lambda Expressions and set a condition inside the ThenBy() method to sort the strings according to the number of characters they have.IEnumerable res = str.AsQueryable().OrderBy(alp => alp.Length).ThenBy(alp => alp);Here is the entire example to order array elements using ThenBy() method.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "Sandler", "Jack", "Tom", "Matt", "Henry", "Johnny" }; IEnumerable ... Read More
Use SelectMany method to collapse elements into a single collection like an error.An example would be to convert string to character array. The following is our string array.string[] str = { "Mobile", "Laptop", "Tablet" };Now, convert to character array.str.SelectMany(item => item.ToCharArray());Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "Mobile", "Laptop", "Tablet" }; var res = str.SelectMany(item => item.ToCharArray()); Console.WriteLine("String converted to character array: "); foreach (char letter in res) { Console.Write(letter); } } }OutputString converted to character array: MobileLaptopTablet
To check whether two sequences are equal or not, use the SequenceEqual() method.Firstly, set the sequences.Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 }; Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }; List employee1 = new List { emp1, emp2 }; List employee2 = new List { emp1, emp2 };Now, find whether the sequences are equal or not.employee1.AsQueryable().SequenceEqual(employee2);Here is the example that shows the result.Exmaple Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { Employee ... Read More
Declare a string array.string [] students = {"Jenifer", "Angelina", "Vera"};Add it to a LinkedList.string [] students = {"Jenifer", "Angelina", "Vera"};Now, use AddLast() method to add a node at the end.list.AddLast("Anne");Example Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Jenifer", "Angelina", "Vera"}; LinkedList list = new LinkedList(students); foreach (var stu in list) { Console.WriteLine(stu); } // add a node at the end Console.WriteLine("Node added at the last..."); list.AddLast("Anne"); ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP