Found 2587 Articles for Csharp

C# Queryable Take() Method

karthikeya Boyini
Updated on 23-Jun-2020 08:32:02

3K+ Views

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

C# Program to find the sum of a sequence

George John
Updated on 23-Jun-2020 08:32:35

600 Views

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

C# Linq Sum() Method

Samual Sam
Updated on 23-Jun-2020 08:32:57

11K+ Views

Find the sum of elements using the Linq Sum() method.Here’s our list with integer elements.List list = new List { 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 Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List list = new List { 99, 34, 77, 75, 87, 35, 88};       int res = list.AsQueryable().Sum();       Console.WriteLine("Sum = {0}", res);    } }OutputSum = 495

C# Program to return the difference between two sequences

Ankith Reddy
Updated on 23-Jun-2020 08:34:54

248 Views

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 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 void Main() {       double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };       double[] arr2 = { 15.6, 30.5, 50.2 };       Console.WriteLine("Initial List...");       foreach(double ele in arr1) {          Console.WriteLine(ele);       }       IEnumerable res = arr1.AsQueryable().Except(arr2);       Console.WriteLine("New List...");       foreach (double a in res) {          Console.WriteLine(a);       }    } }OutputInitial List... 10.2 15.6 23.3 30.5 50.2 New List... 10.2 23.3

C# Enum TryParse() Method

karthikeya Boyini
Updated on 23-Jun-2020 08:36:02

10K+ Views

The TryParse() method converts the string representation of one or more enumerated constants to an equivalent enumerated object.Firstly, set an enum.enum Vehicle { Bus = 2, Truck = 4, Car = 10 };Now, let us declare a string array and set some values.string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" };Now parse the values accordingly using Enum TryParse() method.Example Live Demousing System; public class Demo {    enum Vehicle { Bus = 2, Truck = 4, Car = 10 };    public static void Main() {       string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" ... Read More

C# Queryable SkipWhile() Method

Arjun Thakur
Updated on 23-Jun-2020 08:36:30

152 Views

Bypass elements in an array and return the remaining elements using the SkipWhile() method.The following is our array −int[] marks = { 45, 88, 55, 90, 95, 85 };Now, let’s skip the elements greater than or equal to 60. The condition we have set using Lambda Expression.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 60);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 55, 90, 95, 85 };       // skips elements above 60       IEnumerable selMarks = ... Read More

C# Linq SkipLast Method

Samual Sam
Updated on 23-Jun-2020 08:36:50

2K+ Views

Skip elements from the end and return the remaining elements using the SkipLast() method.The following is an array.int[] marks = { 45, 88, 50, 90, 95, 85 };Now, let us skip two elements from the end using SkipLast() and Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 50, 90, 95, 85 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);       Console.WriteLine("Skipped ... Read More

C# Numeric (“N”) Format Specifier

Chandu yadav
Updated on 23-Jun-2020 08:24:48

2K+ Views

The numeric ("N") format specifier converts a number to a string of the following form −"-d,ddd,ddd.ddd…"Above,"-" is a negative number symbol if required,"d" is a digit (0-9), "," indicates a group separator,"." is a decimal point symbolExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double val1 = -5566.789;       Console.WriteLine(val1.ToString("N", CultureInfo.InvariantCulture));       int val2 = 87987766;       Console.WriteLine(val2.ToString("N3", CultureInfo.InvariantCulture));    } }Output-5,566.79 87,987,766.000

C# Linq Skip() Method

George John
Updated on 23-Jun-2020 08:25:44

3K+ Views

Skip elements and return the remaining elements using the Skip() method.The following is an array.int[] marks = { 80, 55, 79, 99 };Now, let us skip 2 elements using Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);Exampleusing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 80, 55, 79, 99 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);       Console.WriteLine("Skipped the result of 1st two students...");       foreach (int res in selMarks) {          console.WriteLine(res);       }    } }

Removing whitespaces using C# Regex

karthikeya Boyini
Updated on 23-Jun-2020 08:25:19

2K+ Views

Let’s say we want to remove whitespace from the following string str1.string str1 = "Brad Pitt";Now, use Regex Replace to replace whitespace with empty. Here, we have used System.Text.RegularExpressions.string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");Let us see the complete example.Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string str1 = "Brad Pitt";          Console.WriteLine(str1);          string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");          Console.WriteLine(str2);       }    } }OutputBrad Pitt BradPitt

Advertisements