Converting a <int> List to a Complex Type List

The problem is simple, and I'm looking for a simple solution. I have a class with one member like

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

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

and list for example

List <int> lstNumber 

I want to do something like

intList = lstNumber. ``

I know that I can do this using the foreach statement, but I wonder how the class has only one member, i.e. too integer, is there anyway I can convert it directly using something like Linq. I'm just a novice C # programmer, so I really appreciate any help

+4
source share
4 answers

Other answers are good. You can also try LINQ as follows:

intList = (from o in lstNumber select new Test { TestInt = o }).ToList<Test>();
0
source

. Test lstNumber.

var intList = lstNumber.Select(x => new Test{ TestInt = x}).ToList();
+6
intList = lstNumber.Select( i => new Test { TestInt = i } ).ToList();
+4

-

intList = lstNumber.Select(x=> new Test{TestInt=x}).ToList();
+2

Source: https://habr.com/ru/post/1529336/


All Articles