Linq可以将一个int列表投射到另一个T类列表中

 孽尐星_186 发布于 2023-02-12 13:11

我有一个int列表,我想知道我是否可以使用Linq基于以前的列表创建另一个对象T列表.

为了简化问题:

我有一个这样的int列表:1,2,3,4我希望有(1,2),(2,4),(3,6),(4,8)

通常,我们可以在没有Linq的情况下轻松完成

public class T
    {
        int first;
        int Second;
        public T(int x, int y)
        {
            first = x;
            Second = y;
        }
    }


class Program
    {
         static void Main(string[] args)
        {
            List series = new List() { 1,2,3,4 };
            List obj = new List();
            foreach (int item in series)
            {
               obj.Add(new T(item,item*2));
            }
        }
    }

这非常有效.但是当我尝试使用Linq时

List obj = series.Select(x=> {new T(x,x*2)}).ToList();

我认为它会起作用,但我说错了

Error   2   The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我做错了什么?我仍然是一个新手(可能是几个月大)学习Linq :)

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有