C#中linq查询结果怎么赋给list或者Arraylist

如题所述

1. 可以用查询结果的 ToList()方法

2. 示例代码如下:

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 3, 4, 5, 6, 7, 8, 11 };
            List<int> evenList;

            //找出arr中的偶数放入 一个evenList里
            var result = from v in arr where v % 2 == 0 select v;
            evenList = result.ToList();
            
            //显示evenList的结果
            foreach (var v in evenList)
                Console.Write("{0} ", v);
            Console.WriteLine();

            Console.ReadKey();
        }
    }
}

3. 运行结果如下

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-12
直接ToList就行。例如var list=(from temp in xxx select temp.id).ToList();
第2个回答  2013-09-12
toList
第3个回答  2013-09-12
.ToList(),什么类型转换不了?List只是一个容器,容器存放的是什么类型?