Home > Net >  Get an list of lists with multiple attributes an obtain a new list with a specific attribute in C#
Get an list of lists with multiple attributes an obtain a new list with a specific attribute in C#

Time:05-13

I have a problem manipulating lists and I don´t remember very well how to do this...

            var allUsers = _userService.GetAll();

            List<string> result = new List<string>();

The var allUsers gets a list of a lists with this format

-> [[UserName1,X1,Y1],[UserName2,X2,Y2],...]

Note: I also have a db context when I can get a List of Users with Usernames but i need to put that to a new list() as well but i dont remember the methods to do that too

the result is a new List when im try to put all UserNames

-> [UserName1,UserName2,...]

How I do that in C# and if I call a table of any database will be the same??

Ps: I apologize if there are any spelling mistakes and if the question is very simple to answer that it becomes repetitive

CodePudding user response:

You will want to use AddRange of List<T> in conjunction with the Select method found in the System.Linq assembly.

result.AddRange(allUsers.Select(u => u.Username));  

or simply

var result = allUsers.Select(u => u.Username).ToList();
  • Related