找到 34423 篇文章,关于编程

C# Queryable LongCount 方法

Chandu yadav
更新于 2020年6月23日 07:01:10

152 次浏览

使用 Linq LongCount 方法获取元素的计数。以下为我们的字符串数组 -string[] emp = { "Jack", "Mark"};现在,使用 LongCount() 方法。emp.AsQueryable().LongCount();以下是完整代码。示例 在线演示using System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] emp = { "Jack", "Mark"};       long res = emp.AsQueryable().LongCount();       Console.WriteLine("{0} employees in the Department", res);    } }输出2 employees in the Department

C# Linq Distinct() 方法

George John
更新于 2020年6月22日 15:42:04

654 次浏览

要获取不同的元素,请使用 Distinct() 方法。以下是我们包含重复元素的列表。List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };现在要获取不同的元素 -points.AsQueryable().Distinct();让我们看看完整的示例 -示例 在线演示using System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };       // 列表中的不同元素       IEnumerable res = points.AsQueryable().Distinct();       foreach (int a in res) {          Console.WriteLine(a);       }    } }输出5 10 20 30 40 50 60 70

从给定的 C# 字符串中删除所有重复字符

Ankith Reddy
更新于 2020年6月22日 15:43:10

361 次浏览

以下是字符串。string str = "ppqqrr";现在,使用 Hashset 将字符串映射到字符。这将从字符串中删除重复字符。var res = new HashSet(str);让我们看看完整的示例 -示例 在线演示using System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "ppqqrr";          Console.WriteLine("Initial String: "+str);          var res = new HashSet(str);          Console.Write("New String after removing duplicates:");          foreach (char c in res){             Console.Write(c);          }         }    } }输出Initial String: ppqqrr New String after removing duplicates:pqr

C# All 方法

Arjun Thakur
更新于 2020年6月22日 15:43:53

3K+ 次浏览

All 方法检查集合中的所有值并返回布尔值。即使其中一个元素不满足设置的条件,All() 方法也会返回 False。让我们看看一个例子 -int[] arr = {10, 15, 20};现在,使用 All() 方法,我们将检查上述数组中的每个元素是否都大于 5。arr.AsQueryable().All(val => val > 5);示例 在线演示using System; using System.Linq; class Demo {    static void Main() {       int[] arr = {10, 15, 20};       // 检查所有数组元素是否都大于 5       bool res = arr.AsQueryable().All(val => val > 5);       Console.WriteLine(res);    } }输出True

在 C# 中将 Int64 表示为二进制字符串

Chandu yadav
更新于 2020年6月22日 15:44:15

1K+ 次浏览

要在 C# 中将 Int64 表示为二进制字符串,请使用 ToString() 方法并将基数设置为 ToString() 方法的第二个参数,即二进制的 2。Int64 表示一个 64 位有符号整数。首先,设置一个 Int64 变量。long val = 753458;现在,通过将 2 作为第二个参数将其转换为二进制字符串。Convert.ToString(val, 2)示例 在线演示using System; class Demo {    static void Main() {       long val = 753458;       Console.WriteLine("Long: "+val);       Console.Write("Binary String: "+Convert.ToString(val, 2));    } }输出Long: 753458 Binary String: 10110111111100110010

C# Aggregate() 方法

George John
更新于 2020年6月22日 15:44:36

478 次浏览

Aggregate() 方法对序列应用累加器函数。以下是我们的数组 -string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};现在使用 Aggregate() 方法。我们已将 ssed 值设置为“DemoFive”进行比较。string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length > longest.Length ? next : longest, str => str.ToLower());此处,结果字符串应具有比初始种子值(即“DemoFive”)更多的字符数。示例 在线演示using System; using System.Linq; class Demo {    static void Main() {       string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};       string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) ... 阅读更多

C# 中的 Enumerable.Repeat 方法

Ankith Reddy
更新于 2020年6月22日 15:45:10

2K+ 次浏览

Enumerable.Repeat 方法是 System.Linq 命名空间的一部分。它在 C# 中返回一个包含重复元素的集合。首先,设置要重复的元素以及重复次数。例如,让我们看看如何将数字 10 重复五次 -Enumerable.Repeat(10, 5);以下是完整示例 -示例 在线演示using System; using System.Linq; class Demo {    static void Main() {       var val = Enumerable.Repeat(10, 5);       foreach (int res in val) {          Console.WriteLine(res);       }    } }输出10 10 10 10 10

C# 中的 Convert.ToUInt16 方法

Arjun Thakur
更新于 2020年6月22日 15:45:35

192 次浏览

使用 Convert.ToUInt16 方法将指定值转换为 16 位无符号整数。以下是我们的字符串 -string str = "1129";现在让我们将其转换为 16 位无符号整数。ushort res; res = Convert.ToUInt16(str);以下是完整示例 -示例 在线演示using System; public class Demo {    public static void Main() {       string str = "1307";       ushort res;       res = Convert.ToUInt16(str);       Console.WriteLine("Converted string '{0}' to {1}", str, res);    } }输出Converted string '1307' to 1307

C# 中的 Convert.ToInt16 方法

Chandu yadav
更新于 2020年6月22日 15:46:02

678 次浏览

使用 C# 中的 Convert.ToInt16 方法将指定值转换为 16 位有符号整数。我们有一个 double 变量,其值已初始化。double doubleNum = 3.456;现在,让我们将其转换为 Int16,即 short。short shortNum; shortNum = Convert.ToInt16(doubleNum);以下是完整示例 -示例 在线演示using System; public class Demo {    public static void Main() {       double doubleNum = 3.456;       short shortNum;       shortNum = Convert.ToInt16(doubleNum);       Console.WriteLine("Converted {0} to {1}", doubleNum, shortNum);    } }输出Converted 3.456 to 3

在 C# 中对两个列表取交集

George John
更新于 2020年6月22日 15:51:21

8K+ 次浏览

首先,设置两个列表。List val1 = new List { 25, 30, 40, 60, 80, 95, 110 }; List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };现在,使用 Intersect() 方法获取两个列表之间的交集。IEnumerable res = val1.AsQueryable().Intersect(val2);示例 在线演示using System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List val1 = new List { 25, 30, 40, 60, 80, 95, 110 };       List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };       IEnumerable res = val1.AsQueryable().Intersect(val2);       Console.WriteLine("Intersection of both the lists...");       foreach (int a in res) {          Console.WriteLine(a);       }    } }输出Intersection of both the lists... 40 95 110

广告

© . All rights reserved.