有没有一种方法来填充使用LINQ EX pression集合? [英] Is there a way to fill a collection using a LINQ expression?

查看:137
本文介绍了有没有一种方法来填充使用LINQ EX pression集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个有关LINQ的伟大的事情是让你获得多数民众赞成,而无需手动编写code遍历集合与集合的信息。 有没有一种方法使用LINQ来填充的集合,从而避免了需要写一个循环?


例如,假设我有以下的code这与一系列的数字作品从一到十:

 公共静态无效LinqTest()
{
    名单< INT> intList =新的名单,其中,INT>();
    的for(int i = 1; I< = 10;我++)//< ====我在使用一个for循环
        intList.Add(ⅰ); //这里来填充列表。
    INT intSum = intList.Sum();
    INT intSumOdds = intList.Where(X =&X的催化剂%2 == 1).SUM();
    双intAverage = intList.Average();
    Console.WriteLine(总和为{0} \赔率TSUM是{1} \ tAverage为{2},
        intSum,intSumOdds,intAverage);
}
 

LINQ已经取代了循环,将需要为恢复有关数据信息。我很好奇,如果LINQ可以用来替换循环填充数据。有没有一种方法使用LINQ来替换以下两行$​​ C $ C?

 的for(int i = 1; I< = 10;我++)
        intList.Add(ⅰ);
 

解决方案

正如其他人说,你可以使用 Enumerable.Range(INT,INT)来生成整数序列返回的IEnumerable< INT>

虽然你可以将结果转换为名单,其中,INT> 在已经提出已经是各种方式,你只能这样做,如果你确实需要一个名单,其中,T>

在这种情况下,也没有必要这样做。你的函数可以改写如下:

 的IEnumerable< INT> intList = Enumerable.Range(1,10);
INT intSum = intList.Sum();
INT intSumOdds = intList.Where(X =&X的催化剂%2 == 1).SUM();
双intAverage = intList.Average();
 

这是更有效,因为序列由 Enumerable.Range 返回产生懒惰,因为它是枚举。在另一方面,当序列被转换为名单,其中,INT> 那么所有的值必须在内存中举行一次

One of the great things about LINQ is that allows you to get information that's related to a collection without having to manually write code to iterate through the collection. Is there a way to use LINQ to populate a collection, thus avoiding the need to write a loop?


For example, let's say I have the following code which works with a range of numbers from one to ten:

public static void LinqTest()
{
    List<int> intList = new List<int>();
    for (int i = 1; i <= 10; i++)     //  <==== I'm having to use a for loop
        intList.Add(i);               //        here to populate the List.
    int intSum = intList.Sum();
    int intSumOdds = intList.Where(x => x % 2 == 1).Sum();
    double intAverage = intList.Average();
    Console.WriteLine("Sum is {0}\tSum of Odds is {1}\tAverage is {2}", 
        intSum, intSumOdds, intAverage);
}

LINQ is already replacing the for loops that would be required to retrieve information about the data. I'm curious as if LINQ could be used to replace the for loop that populates data. Is there a way to use LINQ to replace the following two lines of code?

    for (int i = 1; i <= 10; i++)
        intList.Add(i);

解决方案

As the others have said, you can use Enumerable.Range(int, int) to generate a sequence of integers returned as IEnumerable<int>.

And while you can convert the result to a List<int> in the various ways that have been suggested already, you should only do that if you actually need a List<T>.

In this case, there is no need to do so. Your function could be rewritten as follows:

IEnumerable<int> intList = Enumerable.Range(1, 10);
int intSum = intList.Sum();
int intSumOdds = intList.Where(x => x % 2 == 1).Sum();
double intAverage = intList.Average();

This is more efficient since the sequence returned by Enumerable.Range is generated "lazily" as it is enumerated. On the other hand, when the sequence is converted to a List<int> then all of the values must be held in memory at once.

这篇关于有没有一种方法来填充使用LINQ EX pression集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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