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
JavaScript array toSource() method returns a string representing the source code of the array. This method is supported by Mozilla.ExampleYou can try to run the following code to learn how to represent the source code of an object with JavaScript Arrays − JavaScript Array toSource Method var arr = new Array("football", "baseball", "volleyball", "cricket"); var str = arr.toSource(); document.write("Returned string is : " + str );
Use the reduce() method in JavaScript to apply a function simultaneously against two values of the array from left-to-right 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 callback.ExampleYou can try to run the following code to learn how to work with reduce() method in JavaScript − JavaScript Array reduce Method if (!Array.prototype.reduce) { ... Read More
Use ReadAllLines() method to read all the lines one by one in a file.Let’s say we have a file “new.txt” with the following lines.One Two ThreeFirstly, set the path of the file to be read.String myPath = "new.txt";Now add it under a string array to fetch the lines on by one.String[] fLine = File.ReadAllLines(myPath);Let’s say you need to fetch the first line. For that.fLine[0]The following is the complete example that reads lines one by one in a file.Exampleusing System; using System.IO; public class Demo { public static void Main() { String myPath = "new.txt"; ... Read More
Get only a single element of a sequence using the Single() method.Let’s say we have a string array with only one element.string[] str = { "one" };Now, get the element.str.AsQueryable().Single();Here is our code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "one" }; string res = str.AsQueryable().Single(); Console.WriteLine(res); } }Outputone
To match any string containing one or more p’s with JavaScript RegExp, use the p+ Quantifier.ExampleYou can try to run the following code to match any string containing one or more p’s. Here p is considered as a number of occurrences − JavaScript Regular Expression var myStr = "Secure and Responsive!"; var reg = /s+/g; var match = myStr.match(reg); document.write(match);
The Single() method returns the only element that satisfies a condition. If more than one such element is visible, then an error is thrown.The following is our string array.string[] str = { "jack", "tom", "henry", "time"};Now, use the Single() method to get each element. Then, we have used Lambda Expression to calculate an element whose length is greater than four.str.AsQueryable().Single(name => name.Length > 4);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "jack", "tom", "henry", "time"}; // finding string whose length is ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP