C# LINQ Select Method

George John
Updated on 23-Jun-2020 08:20:09

2K+ Views

Use the Select method to modify the elements in an array.The following is our string array.string[] stationery = { "diary", "board", "pencil", "whiteboard" };The Select method also specifies Lambda Expressions as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] stationery = { "diary", "board", "pencil", "whiteboard" };       var res = stationery.AsQueryable().Select((item, index) => new { result = item.Substring(0, index + 4) });       foreach (var str in res) {          Console.WriteLine("{0}", str);       }    } }Output{ result = diar } { result = board } { result = pencil } { result = whitebo }

ASEnumerable in C#

Samual Sam
Updated on 23-Jun-2020 08:19:39

5K+ Views

To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method.The following is our array −int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;Now, get the IEnumerable equivalent.arr.AsEnumerable();Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = new int[5];       arr[0] = 10;       arr[1] = 20;       arr[2] = 30;       arr[3] = 40;       arr[4] = 50;       var res = arr.AsEnumerable();       foreach (var ele in res) {          Console.WriteLine(ele);       }    } }Output10 20 30 40 50

Get First Three Letters from Every String in C#

George John
Updated on 23-Jun-2020 08:19:11

3K+ Views

The following are our strings in a list −List list = new List { "keyboard", "mouse", "joystick", "monitor" };To use the first 3 letters, use substring method and use it under the Linq Select method.IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0, 3));Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List list = new List { "keyboard", "mouse", "joystick", "monitor" };       // getting first 3 letters from every string       IEnumerable res = list.AsQueryable() .Cast() .Select(str =>       str.Substring(0,3));       foreach (string str in res) {          Console.WriteLine(str);       }    } }Outputkey mou joy mon

C# LINQ FirstOrDefault Method

Ankith Reddy
Updated on 23-Jun-2020 08:18:42

20K+ Views

Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn’t there.The following is our empty list −List val = new List { };Now, we cannot display the first element, since it is an empty collection. For that, use the FirstorDefault() method to display the default value.val.AsQueryable().FirstOrDefault();The following is the complete example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List val = new List { };       double d = val.AsQueryable().FirstOrDefault();       Console.WriteLine("Default Value = "+d);     ... Read More

LINQ Intersect Method in C#

karthikeya Boyini
Updated on 23-Jun-2020 08:18:12

616 Views

Find common elements between two arrays using the Intersect() method.The following are our arrays −int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 };To perform intersection.val1.AsQueryable().Intersect(val2);Let us see the entire example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       int[] val1 = { 15, 20, 40, 60, 75, 90 };       int[] val2 = { 17, 25, 35, 55, 75, 90 };       IEnumerable res = val1.AsQueryable().Intersect(val2);       Console.WriteLine("Intersection of both the lists...");       foreach (int a in res)       Console.WriteLine(a);    } }OutputIntersection of both the lists... 75 90

Find Size of a Variable Without Using sizeof in C#

Syed Javed
Updated on 23-Jun-2020 08:17:47

2K+ Views

To get the size of a variable, sizeof is used.int x; x = sizeof(int);To get the size of a variable, without using the sizeof, try the following code −// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;Here is the complete code.Example Live Demousing System; class Demo {    public static void Main() {       int x;       // using sizeof       x = sizeof(int);       Console.WriteLine(x);       // without using sizeof       byte[] dataBytes = BitConverter.GetBytes(x);       int d = dataBytes.Length;       Console.WriteLine(d);    } }Output4 4

Set Day of the Month for a Specified Date in Universal Time

Anjana
Updated on 23-Jun-2020 08:17:22

109 Views

JavaScript date setUTCDate() method sets the day of the month for a specified date according to universal time.The following is the parameter for setUTCDate(dayValue) −dayValue − An integer from 1 to 31, representing the day of the month.ExampleYou can try to run the following code to set the day of the month for a specified date −           JavaScript setUTCDate Method                        var dt = new Date( "Oct 15, 2017 23:30:00" );          dt.setUTCDate( 20 );                    document.write( dt );          

C# Regex Matches Method

Samual Sam
Updated on 23-Jun-2020 08:16:49

2K+ Views

The method matches instances of a pattern and is used to extract value based on a pattern.Let us see hoe to check for a valid URL.For that, pass the regex expression in the Matches method.MatchCollection mc = Regex.Matches(text, expr);Above, the expr is our expression that we have set to check for valid URL."^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?”The text we have set to check is a URL i.e.https://demo.comLet us see the complete code.Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       private static void showMatch(string text, string expr) {          MatchCollection mc = Regex.Matches(text, expr);   ... Read More

Set Full Year for Specified Date According to Universal Time

Ramu Prasad
Updated on 23-Jun-2020 08:16:39

146 Views

JavaScript date setUTCFullYear() method sets the full year for a specified date according to universal time.The following are the parameters for setUTCFullYear(yearValue[, monthValue[, dayValue]]) −yearValue − An integer specifying the numeric value of the year, for example, 2008.monthValue − An integer between 0 and 11 representing the months January through December.dayValue − An integer between 1 and 31 representing the day of the month. If you specify the dayValue parameter, you must also specify the monthValue.ExampleYou can try to run the following code to set the full year for a specified date according to universal time −       ... Read More

AsQueryable in C#

Arjun Thakur
Updated on 23-Jun-2020 08:15:40

4K+ Views

AsQueryable() method is used to get an IQueryable reference.Let us see an example to find sum of integer values.Firstly, set an integer array.var arr = new int[] { 100, 200, 300, 400 };Now to find the sum, use the Queryable Sum() and AsQueryable() method.Queryable.Sum(arr.AsQueryable());The following is the complete code.Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       var arr = new int[] { 100, 200, 300, 400 };       int res = Queryable.Sum(arr.AsQueryable());       Console.WriteLine("Sum: "+res);    } }OutputSum: 1000

Advertisements