Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting List<int> to List of complex type

Tags:

c#

linq

generics

Problem is simple and I am looking for a simple solution. I am having a class with single member like

public class Test
{
      public int TestInt{get; set; }
}

List <Test> intList = new List<Test>();

and a list like

List <int> lstNumber 

I want to do something like

intList = lstNumber . ``

I know I can do it using foreach statement but wondering as the class is just having one member i.e. too integer, is there is anyway I can convert it directly by using something like Linq. I am just a beginner programmer in C# so would really appreciate any help

like image 355
user2443648 Avatar asked Dec 16 '22 01:12

user2443648


1 Answers

This should work. You can create a new instance of Test for each integer in lstNumber.

var intList = lstNumber.Select(x => new Test{ TestInt = x}).ToList();
like image 197
Grant Winney Avatar answered Dec 28 '22 01:12

Grant Winney