个性化阅读
专注于IT技术分析

LINQ转Int数组

点击下载

本文概述

LINQ to int Array表示在整数数组上编写LINQ查询, 以从整数数组元素中获取所需的元素。通过使用LINQ, 对整数数组进行查询, 我们可以从整数数组中获取所需数据, 而无需编写太多代码。

LINQ到int数组的语法

在整数数组上编写LINQ查询以从数组集合中获取所需元素的语法。

IEnumerable<int> result = from a in numarr1

                                      select a;

在上面的语法中, 我们编写了LINQ查询以从”数字”整数数组中获取数据。

LINQ to int Array的示例

这是LINQ to int Array从序列中获取元素的示例, 其中元素的值大于10且小于200。

using System;
using System. Collections;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Programme2
    {
        static void Main(string[] args)
        {
//create an array numarray of type int
            int[] numarray = { 1, 6, 9, 10, 50, 60, 100, 200, 300 };
/*write LINQ query to get hte data from the 
numarray where the value is greater than 10 and less than 200*/
            IEnumerable<int> result = from a in numarray
            where a > 10 && a < 200
            select a;
            foreach (int item in result)
            {
                Console.WriteLine(item);
            }
                Console.ReadLine();
        }
    }
}

在上面的代码中, 我们对” numarray”整数数组使用了LINQ查询, 以获取其值大于10且小于200的元素。

输出

LINQ转Int数组

赞(0)
未经允许不得转载:srcmini » LINQ转Int数组

评论 抢沙发

评论前必须登录!