C# Program to order array elements in descending order

Samual Sam
Published on 14-Sep-2018 15:56:32
To orders elements in a sequence in descending order, use ThenBy() and OrderByDescending.This is our string array.string[] myStr = { "Keyboard", "Laptop", "Mouse", "Monitor" };Now, use OrderByDescending to order element in Descending order. Inside that calculate the length of each string and use Lambda Expressions as well.IEnumerable<string> res = myStr.AsQueryable().OrderByDescending(ch ... Read More

C# Linq ThenBy Method

Ankith Reddy
Published on 14-Sep-2018 15:56:08
Orders elements in a sequence using ThenBy() method.We have the following string array.string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };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<string> res = str.AsQueryable() .OrderBy(alp => ... Read More

C# Program to order array elements

karthikeya Boyini
Published on 14-Sep-2018 15:52:41
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<string> res = ... Read More

Represent Int64 as a Octal string in C#

Arjun Thakur
Published on 14-Sep-2018 15:52:43
To reprsent Int64 as a Octal string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 8 for Octal.Int64 represents a 64-bit signed integer.Firstly, set an Int64 variable.long val = 986766;Now, convert it to octal string by including 8 as the second ... Read More

C# Linq TakeWhile() Method

Samual Sam
Published on 14-Sep-2018 15:48:34
Get elements as long as the condition is true in a sequence using the TakeWhile() method.The following is our list with strings.IList<string> str = new List<string>(){ "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 ... Read More

C# Queryable TakeLast() Method

Chandu yadav
Published on 14-Sep-2018 15:48:11
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 ... Read More

C# Queryable Take() Method

karthikeya Boyini
Published on 14-Sep-2018 15:44:46
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 ... Read More

C# Program to find the sum of a sequence

George John
Published on 14-Sep-2018 15:44:23
Firstly, set a sequence.List<int> myList = new List<int> { 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<int> myList = new List<int> { 1, 2, 3, ... Read More

C# Linq Sum() Method

Samual Sam
Published on 14-Sep-2018 15:41:17
Find the sum of elements using the Linq Sum() method.Here’s our list with integer elements.List<int> list = new List<int> { 99, 34, 77, 75, 87, 35, 88};Now find the sum using the Sum() method.list.AsQueryable().Sum();The following is an example to find the sum of elements of a list with integer elements.Example Live ... Read More

C# Program to return the difference between two sequences

Ankith Reddy
Published on 14-Sep-2018 15:41:07
Set two sequences.double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 }; double[] arr2 = { 15.6, 30.5, 50.2 };To get the difference between both the above arrays, use Except() method.IEnumerable<double> res = arr1.AsQueryable().Except(arr2);The following is the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static ... Read More
Advertisements