使用 WHERE 子句 LINQ 查找数组中最大数的 C# 程序
LINQ 被称为语言集成查询,它是在 .NET 3.5 中引入的。它使.NET语言能够生成查询以从数据源中检索数据。它消除了编程语言和数据库之间的不匹配,并且无论使用哪种类型的数据源,用于创建查询的语法都是相同的。在本文中,我们将学习如何使用 WHERE 子句 LINQ 查找数组中的最大数。在这里,我们将获得大于给定数组中特定数字的数字。
例子:
Input: Array of Integers: 100,200,300,450,324,56,77,890
Value: 500
Output: Numbers greater than 500 are: 890
Input: Array of Integers: 34,56,78,100,200,300,450,324,56,77,890
Value: 100
Output: Numbers greater than 100 are: 200,300,450,324,890
方法:
To display the greatest numbers in an array using WHERE Clause LINQ follow the following approach:
- Store integer(input) in an array.
- The sum of the elements is calculated using the for loop.
- The numbers which are greater than particular value is checked using where function.
- By using the LINQ query we will store the numbers in an iterator.
- Now the iterator is iterated and the integers are printed.
例子:
C#
// C# program to print the greatest numbers in an array
// using WHERE Clause LINQ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class GFG{
static void Main()
{
// Array of numbers
int[] array1 = { 34, 56, 78, 100, 200, 300,
450, 324, 56, 77, 890 };
// Now get the numbers greater than 100 and
// store in big variable using where clause
var big = from value in array1 where value > 100 select value;
Console.WriteLine("Numbers that are greater than 100 are :");
// Get the greater numbers
foreach (var s in big)
{
Console.Write(s.ToString() + " ");
}
Console.Read();
}
}
输出:
Numbers that are greater than 100 are :
200 300 450 324 890