初始化列表< INT>与LINQ查询 [英] Initialize a List<int> with LINQ query

查看:216
本文介绍了初始化列表< INT>与LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我初始化列表< INT> 通过存储两个简单的常数,MINVALUE和MaxValue的这样一个构造函数:

I am initializing a List<int> in a constructor by storing two simple constants, MinValue and MaxValue as such:

private const int MinValue = 1;
private const int MaxValue = 100;

private List<int> integerList = new List<int>();

public Class()
{
    for (int i = MinValue ; i < MaxValue ; i++)
    {
        integerList .Add(i);
    }
}



有没有办法,只是初始化一个列表简单的LINQ查询?由于列表< T> 可以构造一个的IEnumerable< T> ,并以下形式的查询存在?

Is there a way to just initialize the list with a simple LINQ query? Since a List<T> can be constructed with an IEnumerable<T>, does a query of the following form exist?

private List<int> integerList = new List<int>(<insert query here>);



这甚至可能吗?

Is this even possible?

推荐答案

这可以使用的 Enumerable.Range

This can be achieved using Enumerable.Range

List<int> integerList = Enumerable.Range(MinValue, MaxValue - MinValue).ToList();

这篇关于初始化列表&LT; INT&GT;与LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆