关于LINQ list的转换问题
List<Trade> data=getData();
var result =data.Select(x => new { NumIid = x.NumIid.ToString()+"num"});
List<Trade> li = result.ToList();//报错
无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.List<Top.Api.Domain.Trade>”
请问以上的问题应该如何处理
------解决方案--------------------var result =data.Select(x => new
Top.Api.Domain.Trade() { NumIid = x.NumIid.ToString()+"num"});
------解决方案--------------------List<Trade> data=getData();
var result =data.Select(x => new { NumIid = x.NumIid.ToString()+"num"});
List<Trade> li = result.ToList();//报错
result是匿名类型 怎么可能转为Trade呢?
解决方法有下面两种
1、x => new Trade{}
2、定义新的实体类 TradeViewModel
var result =data.Select(x => new TradeViewModel{ NumIid = x.NumIid.ToString()+"num"});
List<TradeViewModel> li = result.ToList();
------解决方案--------------------var result =data.Select(x => new { NumIid = x.NumIid.ToString()+"num"});
你这用的是匿名类,不要用匿名类