我有一个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) { Listseries = new List () { 1,2,3,4 }; List obj = new List (); foreach (int item in series) { obj.Add(new T(item,item*2)); } } }
这非常有效.但是当我尝试使用Linq时
Listobj = 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 :)